Question

Give the output of the following program. Then explain what the foo() method does, in general,...

Give the output of the following program. Then explain what the foo() method does, in general, for a given input. Is foo() a tail-recursive method? Why or why not?

class SomeThing {

    public static void main(String args[]) {

        System.out.println(foo(6));

    }

    private static int foo(int input) {

        if (input <= 0) {

            throw new RuntimeException("invalid input");

        }

        else if (input == 1) {

            return 1;

        }

        else if (input % 2 == 0) {

            return foo(input - 1);

        }

        else {

            return input * foo(input-2);

        }

    }

}

Homework Answers

Answer #1

OUTPUT:

15

Explaination:

5 * 3 * 1 = 15

The above program gives the multiplication of all odd numbers less than the number passed while initially calling the function.

Tracing:

Step 1: foo(6)

Step 2: foo(6-1) [since (6%2) == 0, so, this function is going to call recursively passing input-1]

Step 3: 5 * foo(5-2) [Here, else part runs, It is going to multiply the input with foo(input -2)]

Step 4: 5 * 3 * foo(1) [Here, foo(1) is going to return 1]

Step 5: 5 * 3 * 1 [The final value is returned]

Step 6: 15 [Output]

This function is not a tail-recursive function.

The function is not tail recursive because the value returned by foo(input-2) from Step3 is used in computation in the further steps.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
What does the following application print and explain why it happens? public class Foo {            ...
What does the following application print and explain why it happens? public class Foo {             public void crazyArray(int[] vals){                   int[] newVals = { 1, 7, 6, 1, 3 };                   vals[0] -= vals[0] - newVals[3];                   vals[3] = vals[2] + vals[1] - newVals[4];                   vals[2] -= vals[0] *2;                   vals = newVals;                   vals[1] -= vals[2];                   newVals[2] *= 3 + vals[3];                   vals[1] -= 14 + vals[2];             }                         public static void main(String[] args) {...
int a =12; a += 3; System.out.println(a); // What is the output of the above program?-----...
int a =12; a += 3; System.out.println(a); // What is the output of the above program?----- --------------- the Java keyword "new" accomplishes what? starts a new program gets input creates an object in memory refresh variable values ------------------------ Every Java Class MUST contain the main method: 'public static void main(String[]args)'   true/false
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
Analyze this code and run it, if there are any issues note them and fix them,...
Analyze this code and run it, if there are any issues note them and fix them, if not give the output and Big O notation runtime: public class PrintBits { public static void printBits(int a) { try { String str = Integer.toBinaryString((Integer) a); for(int i = 0; i < str.length(); i++){ System.out.print (str.charAt(i)); } } catch (ClassCastException e) { throw new RuntimeException ("Argument is not an Integer"); } } public static void main (String[] args){ printBits (-17); System.out.println(); printBits (17);...
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is valid. Input: Interactive Output: Valid date is printed or user is alerted that an invalid date was entered. */ import java.util.Scanner; public class BadDate { public static void main(String args[]) { // Declare variables Scanner userInput = new Scanner (System.in); String yearString; String monthString; String dayString; int year; int month; int day; boolean validDate = true; final int MIN_YEAR = 0, MIN_MONTH = 1,...
Write a Java program that implements a lexical analyzer, lex, a recursive-descent parser, parse, an error...
Write a Java program that implements a lexical analyzer, lex, a recursive-descent parser, parse, an error handling program, error, and a code generator, codegen, for the following EBNF description of a simple assignment statement using an arithmetic expression language. Implement a symbol table by modifying Evaluate class. Modify the stmt() method, Provide an insert() and retrieve() method and store values in symbol table.   Video Help to get started:  https://www.youtube.com/watch?v=-C_S7Lb4kRk&feature=youtu.be import java.io.*; import java.util.*; public class parser { static String inString =...
There is a Java program that is missing one recursive function: public class Fibonacci { /*...
There is a Java program that is missing one recursive function: public class Fibonacci { /* / 0 when n = 0 * fib(n) = | 1 when n = 1 * \ fib(n-1)+fib(n-2) otherwise */ public static int fib(int n) { return 0; } /* Fibonacci Test Framework * * Note, this takes a long time to compute fib(44). */ public static void main(String[] args) { int[] input = { 11, 22, 33, 44}; int[] expect = { 89,...