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...
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,...
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,...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
Java program question! If I pass null to a method, how can I use the passed...
Java program question! If I pass null to a method, how can I use the passed value to compare. Here is example code, it might be wrong, please make it could run on eclipse. Public class hw{ public static void main (String args[]) { method1(); } private static void method1() { System.out.println(method(null)); } public static String method(int[] a) { return null; } What I want to get in the output is just "()". It is not just to type the...