quiz7
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)?
18
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)?
5
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)?
8
How many recursive calls to the fib method shown below would be made from an original call to fib(4)? (Do not count the original call) public int fib(int n) { // assumes n >= 0 if (n <= 1) { return n } else { return (fib(n - 1) + fib(n - 2)); } }
8
Consider the getArea method 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 replaced with this: if (width == 1) { return 1; } // replacement for lines #1 and #2 What will happen when this code is executed?
A negative or zero width would cause problems.
Which statement(s) about recursion are true? I Recursion is faster than iteration. II Recursion is often easier to understand than iteration. III Recursive design has an economy of thought.
II and III only
Which of the following executions represent mutual recursion? I method E calls method T, which calls method F II method E calls method T, which calls method F, which calls method E III method F calls method F
II only
____ recursion can occur when a recursive algorithm does not contain a special case to handle the simplest computations directly.
Infinite
Consider the code for the recursive method mystery shown in this code snippet: public static int mystery(int n) { if (n == 0) { return 0; } else { return (n + mystery(n-1)); } } What will be printed by the statement System.out.println(mystery(-4));?
Nothing - a StackoverflowError exception will occur
Consider the getArea method 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?
The method will still return correct results for all non-negative width triangles.
Consider the getArea method 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?
Those with width greater than or equal to 2.
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 parameter values for n would cause an infinite recursion problem in the following method?
all n with n < 0
Complete the code for the calcPower recursive method shown below, which is intended to raise the base number passed into the method to the exponent power passed into the method: public static int calcPower(int baseNum, int exponent) { int answer = 0; if (exponent == 0) { _____________________ } else { answer = baseNum * calcPower (baseNum, exponent - 1); } return answer; }
answer = 1;
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.
i == 0
In recursion, the recursive call is analogous to a loop ____.
iteration
Consider 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) // line #1 { return 0; // line #2 } else { return (n + printSum(n)); //line #3 } } Which of the following statements is correct?
line #3 is incorrect, and should be changed to return (n + printSum(n - 1));
Consider the getArea method 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)?
line #4
Consider the getArea method 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)?
lines #1 and #2
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?
n <= 0 or n == 1
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 in this recursive method.
n <= 1
A recursive method without a special terminating case would _________
never terminate.
Complete the code for the myFactorial recursive method shown below, which is intended to compute the factorial of the value passed to the method: public int myFactorial(int anInteger) { if (anInteger == 1) { return 1; } else { ______________________ } }
return (anInteger * (myFactorial(anInteger - 1)));
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 { ______________________________ } }
return (n + printSum(n - 1));
Insert the missing code in the following code fragment. This fragment is intended to recursively compute xn, where x and n are both non-negative integers: public int power(int x, int n) { if (n == 0) { ____________________ } else { return x * power(x, n - 1); } }
return 1;
Complete the code for the recursive method shown below, which is intended to compute the sum of the first n positive integers: public int s(int n) { if (n == 1) { return 1; } else { _________________ } }
return n + s(n - 1);