Try out our new practice tests completely free! # Quiz 13: Recursion   Consider the following code snippet for calculating Fibonacci numbers recursively: Int fib(int n) { // assumes n >= 0 If (n <= 1) { Return n; } Else { Return (fib(n - 1) + fib(n - 2)); } } Identify the terminating condition. (Multiple Choice)

B Complete the code for the recursive method printSum shown in this code snippet, which is intended to return the sum of digits from 1 to n: public static int printSum(int n) { If (n == 0) { Return 0; } Else { ______________________________ } } (Multiple Choice)

C Consider the recursive method myPrint shown in this code snippet: public void myPrint(int n) { If (n < 10) { System.out.print(n); } Else { Int m = n % 10; System.out.print(m); MyPrint(n / 10); } } What does this method do? (Multiple Choice)

B Consider the recursive method shown below: public static int strangeCalc(int bottom, int top) { If (bottom > top) { Return -1; } Else if (bottom == top) { Return 1; } Else { Return bottom * strangeCalc(bottom + 1, top); } } What value will be returned with a call to strangeCalc(4,7)?
(Multiple Choice) Consider the getArea method from the textbook shown below: public int getArea() { If (width <= 0) { return 0; } // line #1 Else if (width == 1) { return 1; } // line #2 Else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 Int smallerArea = smallerTriangle.getArea(); // line #4 Return smallerArea + width; // line #5 } } Assume the code in line #3 is changed to: Triangle smallerTriangle = new Triangle(width); This change would cause infinite recursion for which triangles?
(Multiple Choice) Consider the getArea method from the textbook shown below. public int getArea() { If (width <= 0) { return 0; } // line #1 Else if (width == 1) { return 1; } // line #2 Else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 Int smallerArea = smallerTriangle.getArea(); // line #4 Return smallerArea + width; // line #5 } } Where is/are the recursive call(s)?
(Multiple Choice) Consider the recursive method myPrint in this code snippet: public void myPrint(int n) { If (n < 10) { System.out.print(n); } Else { Int m = n % 10; System.out.print(m); MyPrint(n / 10); } } What is printed for the call myPrint(821)?
(Multiple Choice) Consider the getArea method from the textbook shown below. public int getArea() { If (width <= 0) { return 0; } // line #1 Else if (width == 1) { return 1; } // line #2 Else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 Int smallerArea = smallerTriangle.getArea(); // line #4 Return smallerArea + width; // line #5 } } Where is/are the terminating condition(s)?
(Multiple Choice) Consider the getArea method from the book shown below. public int getArea() { If (width <= 0) { return 0; } // line #1 Else if (width == 1) { return 1; } // line #2 Else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 Int smallerArea = smallerTriangle.getArea(); // line #4 Return smallerArea + width; // line #5 } } Assume lines #1 and #2 were changed to this: If (width == 1) { return 1; } // new line #1-2 What will happen when this code is executed?
(Multiple Choice) Consider the getArea method from the textbook shown below. public int getArea() { If (width <= 0) { return 0; } // line #1 Else if (width == 1) { return 1; } // line #2 Else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 Int smallerArea = smallerTriangle.getArea(); // line #4 Return smallerArea + width; // line #5 } } Assume line #1 is replaced with this line: If (width <= 0) {return width;} What will be the result?
(Multiple Choice) Consider the following recursive code snippet: public static int mystery(int n, int m) { If (n <= 0) { Return 0; } If (n == 1) { Return m; } Return m + mystery(n - 1, m); } Identify the terminating condition(s) of method mystery?
(Multiple Choice) Consider the recursive method shown below: public static int strangeCalc(int bottom, int top) { If (bottom > top) { Return -1; } Else if (bottom == top) { Return 1; } Else { Return bottom * strangeCalc(bottom + 1, top); } } What value will be returned with a call to strangeCalc(2,3)?
(Multiple Choice) Consider the code for the recursive method myPrint shown in this code snippet: public static int myPrint(int n) { If (n == 0) { Return 0; { Else { Return (n + myPrint(n - 1)); } } To avoid infinite recursion, which of the following lines of code should replace the current terminating case?
(Multiple Choice) Consider the following recursive code snippet: public int mystery(int n, int m) { If (n == 0) { Return 0; } If (n == 1) { Return m; } Return m + mystery(n - 1, m); } What value is returned from a call to mystery(1,5)?
(Multiple Choice) Consider the following recursive code snippet: public int mystery(int n, int m) { If (n == 0) { Return 0; } If (n == 1) { Return m; } Return m + mystery(n - 1, m); } What value is returned from a call to mystery(3,6)?
(Multiple Choice) Consider the following code snippet for recursive addition: Int add(int i, int j) { // assumes i >= 0 If (i == 0) { Return j; } Else { Return add(i - 1, j + 1); } } Identify the terminating condition in this recursive method.
(Multiple Choice) What is required to make a recursive method successful? I special cases that handle the simplest computations directly II a recursive call to simplify the computation III a mutual recursion
(Multiple Choice) Consider the getArea method from the textbook shown below: public int getArea() { If (width <= 0) { return 0; } // line #1 If (width == 1) { return 1; } // line #2 Triangle smallerTriangle = new Triangle(width - 1); // line #3 Int smallerArea = smallerTriangle.getArea(); // line #4 Return smallerArea + width; // line #5 } Which line has the recursive case?
(Multiple Choice) Consider the recursive method myPrint: public void myPrint(int n) { If (n < 10) { System.out.print(n); } Else { Int m = n % 10; System.out.print(m); MyPrint(n / 10); } } What is printed for the call myPrint(8)?
(Multiple Choice) Consider the code for the recursive method mysteryPrint shown in this code snippet: public static int mysteryPrint(int n) { If (n == 0) { Return 0; } Else { Return (n + mysteryPrint(n-1)); } } What will be printed with a call to mysteryPrint(-4)?
(Multiple Choice) ## Filters

• Essay(0)
• Multiple Choice(0)