Deck 13: Recursion

Full screen (f)
exit full mode
Question
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)?

A) 1
B) 5
C) 6
D) 11
Use Space or
up arrow
down arrow
to flip the card.
Question
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
{
______________________________
}
}

A) return (printSum(n - 1));
B) return (n + printSum(n + 1));
C) return (n + printSum(n - 1));
D) return (n - printSum(n - 1));
Question
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?

A) Prints a positive int value forward, digit by digit.
B) Prints a positive int value backward, digit by digit.
C) Divides the int by 10 and prints out its last digit.
D) Divides the int by 10 and prints out the result.
Question
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?

A) line #1
B) line #2
C) line #3
D) line #4
Question
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)?

A) 3
B) 6
C) 18
D) 729
Question
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?

A) Those with width equal to 0.
B) Those with width equal to 1.
C) Those with width greater than or equal to 2.
D) Triangles of any width.
Question
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)?

A) 0
B) -10
C) -22
D) Nothing - a StackoverflowError exception will occur
Question
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?

A) A negative or zero width would cause problems.
B) Nothing - the method would still be correct.
C) We would lose our only recursive case.
D) A positive width would reduce the correct area by 1.
Question
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

A) I
B) II
C) I and II
D) I, II, and III
Question
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.

A) n < 1
B) n <= 1
C) fib(n - 1)
D) fib(n - 1) + fib(n - 1)
Question
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)?

A) 1
B) -1
C) 120
D) 840
Question
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)?

A) line #1
B) line #2
C) lines #1 and #2
D) line #4
Question
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)?

A) 10
B) 8
C) 4
D) 21
Question
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?

A) n <= 0
B) n == 1
C) n <= 0 or n == 1
D) n > 0
Question
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)?

A) line #1
B) line #2
C) lines #1 and #2
D) line #4
Question
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?

A) if (n == -1)
B) if (n <= 0)
C) if (n >= 0)
D) The terminating case as shown will avoid infinite recursion.
Question
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)?

A) 821
B) 128
C) 12
D) 10
Question
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)?

A) 1
B) 2
C) 6
D) 24
Question
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.

A) if (i == 0)
B) return j
C) return add(i - 1, j + 1)
D) there is no terminating condition
Question
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?

A) The method will still return correct results for all non-negative width triangles.
B) The method will return incorrect results for triangles with width equal to 0.
C) The method will return incorrect results for triangles with width equal to 1.
D) The method will return area to be one too high for all triangles.
Question
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?

A) n == 0
B) n == 1
C) all n with n < 0
D) all n with n >= 0
Question
Which of the following options could be used as a terminating condition for a recursive method that finds the middle character of a String with any number of characters?
I the length of the String is 1
II first and last String characters match
III the String is not empty

A) I
B) II
C) I, II and III
D) I and III
Question
Consider the method powerOfTwo shown below:
Public boolean powerOfTwo(int n)
{
If (n == 1) // line #1
{
Return true;
}
Else if (n % 2 == 1) // line #2
{
Return false;
}
Else
{
Return powerOfTwo(n / 2); // line #3
}
}
How many recursive calls are made from the original call of powerOfTwo(64) (not including the original call)?

A) 8
B) 6
C) 4
D) 2
Question
If a recursive method does not simplify the computation within the method and the base case is not called, what will be the result?

A) The terminating condition will be executed and recursion will end.
B) The recursion calculation will occur correctly regardless.
C) This cannot be determined.
D) Infinite recursion will occur.
Question
When a recursive method is called, and it does not perform recursion, what must be true?

A) The terminating condition was true.
B) One recursive case condition was true.
C) All recursive case conditions were true.
D) An exception will occur in the method.
Question
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)
{
_____________________________
{
Return 1;
}
Else
{
Return(anInteger * myFactorial(anInteger - 1));
}
}

A) if (anInteger == 1)
B) if ((anInteger - 1) == 1)
C) if (anInteger * (anInteger - 1) == 1)
D) if (myFactorial(anInteger) == 1)
Question
Consider the getArea method from the textbook shown below:
Public int getArea()
{
If (width <= 0) { return 0; } // line #1
Triangle smallerTriangle = new Triangle(width - 1); // line #2
Int smallerArea = smallerTriangle.getArea(); // line #3
Return smallerArea + width; // line #4
}
If line#1 was removed, what would be the result?

A) The recursive method would cause an exception for values below 0.
B) The recursive method would construct triangles whose width was negative.
C) The recursive method would terminate when the width reached 0.
D) The recursive method would correctly calculate the area of the original triangle.
Question
Consider the method powerOfTwo shown below:
Public boolean powerOfTwo(int n)
{
If (n == 1) // line #1
{
Return true;
}
Else if (n % 2 == 1) // line #2
{
Return false;
}
Else
{
Return powerOfTwo(n / 2); // line #3
}
}
How many recursive calls are made from the original call powerOfTwo(63) (not including the original call)?

A) 6
B) 4
C) 1
D) 0
Question
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);
}
}

A) return 1;
B) return x;
C) return power(x, n - 1);
D) return x * power(x, n - 1);
Question
Consider the method powerOfTwo shown below:
Public boolean powerOfTwo(int n)
{
If (n == 1) // line #1
{
Return true;
}
Else if (n % 2 == 1) // line #2
{
Return false;
}
Else
{
Return powerOfTwo(n / 2); // line #3
}
}
What is the best interpretation of line #1?

A) One is a power of two
B) One is not a power of two
C) Any multiple of one is a power of two
D) 1 is an invalid choice for n
Question
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
}
If line #1 was eliminated from the method, what would be the result when calling getArea?

A) A negative or zero width would cause problems.
B) Nothing - the method would still work correctly.
C) We would lose our only recursive case.
D) A positive width would reduce the correct area by 1.
Question
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));
}
}

A) 1
B) 2
C) 4
D) 8
Question
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
}
Assume line #3 is changed to this:
Triangle smallerTriangle = new Triangle(width + 1)
When calling the getArea method on a Triangle object with width = 4, what result will be produced?

A) area for all triangles will be computed to be too high
B) area for all triangles will be computed to be too low
C) area will only be incorrect for a triangle objects with width = 1
D) infinite recursion will occur for triangle objects with width >= 2
Question
____ recursion can occur when a recursive algorithm does not contain a special case to handle the simplest computations directly.

A) Mutual
B) Non-mutual
C) Terminating condition
D) Infinite
Question
A recursive method without a special terminating case would _________

A) end immediately.
B) not be recursive.
C) be more efficient.
D) never terminate.
Question
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
}
Assume that line #2 is changed to this:
If (width == 1) { return 2; }
How would this affect calls to getArea?

A) It would add 1 to all calls except those where width <= 0.
B) It would make no difference to any call.
C) It would double every triangle area.
D) It would subtract 1 from all calls.
Question
If recursion does not have a special terminating case, what error will occur?

A) Index out of range
B) Illegal argument
C) Stack overflow
D) Out of memory
Question
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
}
Assume that line #3 is changed to this:
Triangle smallerTriangle = new Triangle(width);
This would cause infinite recursion for ____.

A) triangles with width equal to 0
B) triangles with width equal to 1
C) triangles with width greater than or equal to 2
D) triangles of any width
Question
Would switching the special case order affect the return value of the following method?
Public int mystery(int n, int m)
{
If (n == 0) // special case #1
{
Return 0;
}
If (n == 1) // special case #2
{
Return m;
}
Return m + mystery(n - 1, m);
}

A) No
B) Yes
C) It is impossible to tell.
D) An exception will be thrown.
Question
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
{
______________________
}
}

A) return(anInteger * (myFactorial(anInteger)));
B) return ((anInteger - 1) * (myFactorial(anInteger)));
C) return(anInteger * (myFactorial(anInteger - 1)));
D) return((anInteger - 1)*(myFactorial(anInteger - 1)));
Question
Given the following class code:
Public class RecurseMore
{
Private static int total;
Public static void main(String[] args)
{
System.out.println(recurse(4));
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;
}
Else
{
Total = 4 + recurse(n - 2);
}
Return total;
}
}
What values will be printed when this code is executed?

A) 0, 4, and 8
B) 4 and 8
C) 4
D) 8
Question
Complete the following code snippet, which is intended to be a recursive method that will find the smallest value in an array of double values from index to the end of the array:
Public static double minVal(double[] elements, int index)
{
If (index == elements.length - 1)
{
Return elements[index];
}
Double val = __________________;
If (elements[index] < val)
{
Return elements[index];
}
Else
{
Return val;
}
}

A) minVal(elements, index - 1)
B) minVal(index - 1)
C) minVal(elements, index + 1)
D) minVal(index + 1)
Question
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)
{
Answer = 1;
}
Else
{
_______________________________________
}
Return answer;
}

A) answer = baseNum * calcPower (baseNum -1, exponent);
B) answer = baseNum * calcPower (baseNum, exponent - 1);
C) answer = baseNum * calcPower (baseNum, exponent);
D) answer = baseNum * calcPower (baseNum -1, exponent - 1);
Question
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;
}

A) answer = 0;
B) answer = 1;
C) answer = -1;
D) answer = calcPower(1);
Question
Complete the following code snippet, which is intended to be a recursive method that reverses a String value:
Public static String reverseIt(String s)
{
If
{
Return s;
}
Else
{
________________________
}
}

A) return reverseIt;
B) return reverseIt;
C) return reverseIt;
D) return reverseIt;
Question
Given the following code snippet:
Public static int newCalc(int n)
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (1 + newCalc(n / 10));
}
}
What value will be returned when this code is executed with a call to newCalc(15)?

A) 2
B) 2.5
C) 5
D) 5.5
Question
Given the following code snippet:
Public static int newCalc(int n)
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (n % 10) + newCalc(n / 10);
}
}
What value will be returned when this code is executed with a call to newCalc(5)?

A) 2
B) 2.5
C) 5
D) 5.5
Question
Complete the following code snippet, which is intended to be a recursive method that will find the smallest value in an array of double values from index to the end of the array:
Public static double minVal(double[] elements, int index)
{
If (index == elements.length - 1)
{
__________________
}
Double val = minVal(elements, index + 1);
If (elements[index] < val)
{
Return elements[index];
}
Else
{
Return val;
}
}

A) return elements[index];
B) return 1;
C) return 0;
D) return elements[0];
Question
Given the following class code:
Public class RecurseSample
{
Public static void main(String[] args)
{
System.out.println(recurse(3));
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;
}
Else
{
Total = 3 + recurse(n - 1);
}
Return total;
}
}
What values will be printed when this code is executed?

A) 6
B) 9
C) 3, 6, and 9
D) 1, 3, 6, and 9
Question
Complete the following code snippet, which is intended to be a recursive method that reverses a String value:
Public static String reverseIt(String s)
{
If
{
_________
}
Else
{
Return reverseIt;
}
}

A) return s;
B) return 0;
C) return s.charAt(0);
D) return s.substring(1);
Question
Given the following code snippet:
Public static int newCalc(int n)
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (1 + newCalc(n / 10));
}
}
What value will be returned when this code is executed with a call to newCalc(5)?

A) 1
B) 1.5
C) 5
D) 5.5
Question
Complete the following code snippet, which is intended to be a recursive method that will find the sum of all elements in an array of double values from index to the end of the array:
// return the sum of all elements in arr[]
Public static double findSum(double arr[], int index)
{
If (index == 0)
{
_____________________
}
Else
{
Return (arr[index] + findSum(arr, index - 1));
}
}
Assume that this method would be called using an existing array named myArray as follows:
FindSum(myArray,myArray.length - 1);

A) return arr[index];
B) return arr[index + 1];
C) return arr[1];
D) return arr[index - 1];
Question
Given the following class code:
Public class RecurseSample
{
Public static void main(String[] args)
{
Recurse(3);
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;}
Else
{
Total = 3 + recurse(n - 1);
}
System.out.println(total);
Return total;
}
}
What values will be printed?

A) 1, 3, and 6
B) 1, 3, 6, and 9
C) 3, 6, and 9
D) 3, 6, 9, and 12
Question
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)
{
______________________
}
Else
{
Return (anInteger * myFactorial(anInteger - 1));
}
}

A) return 0;
B) return -anInteger;
C) return 1;
D) return myFactorial(anInteger);
Question
Complete the following code snippet, which is intended to be a recursive method that will find the sum of all elements in an array of double values from index to the end of the array:
// return the sum of all elements in arr[]
Public static double findSum(double arr[], int index)
{
If (index == 0)
{
Return arr[index];
}
Else
{
_____________________
}
}
Assume that this method would be called using an existing array named myArray as follows:
FindSum(myArray, myArray.length - 1);

A) return (findSum(arr, index + 1));
B) return (findSum(arr, index - 1));
C) return (arr[index] + findSum(arr, index + 1));
D) return (arr[index] + findSum(arr, index - 1));
Question
Complete the code for the recursive method shown below, which is intended to compute the sum of the first n integers:
Public int s(int n)
{
If (n == 1)
{
Return 1;
}
Else
{
_________________
}
}

A) return n + (n - 1);
B) return s(n) + n - 1;
C) return n + s(n - 1);
D) return n + s(n + 1);
Question
Given the following class code:
Public class RecurseMore
{
Public static void main(String[] args)
{
Recurse(4);
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;
}
Else
{
Total = 4 + recurse(n - 2);
}
System.out.println(total);
Return total;
}
}
What values will be printed when this code is executed?

A) 0, 4, and 8
B) 4 and 8
C) 4
D) 8
Question
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 #
}
Else
{
Return (n + printSum(n)); //line #3
}
}
Which of the following statements is correct?

A) line #1 is incorrect, and should be changed to if (n <= 1)
B) line #3 is incorrect, and should be changed to return (printSum (n - 1));
C) line #3 is incorrect, and should be changed to return (n + printSum (n + 1));
D) line #3 is incorrect, and should be changed to return (n + printSum (n - 1));
Question
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;
________________________
{
Answer = 1;
}
Else
{
Answer = baseNum * calcPower (baseNum, exponent - 1);
}
Return answer;
}

A) if (exponent == 0)
B) if (exponent == 1)
C) if (exponent == -1)
D) if (exponent != 1)
Question
Given the following code snippet:
Public static int newCalc(int n)
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (n % 10) + newCalc(n / 10);
}
}
What value will be returned when this code is executed with a call to newCalc(15)?

A) 2
B) 2.5
C) 6
D) 6.5
Question
Consider the iterative version of the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Long fold = 1;
Long fold2 = 1;
Long fnew = 1;
For (int i = 3; i <= n; i++)
{
Fnew = fold + fold2;
Fold2 = fold;
Fold = fnew;
}
Return fnew;
}
How many iterations of the for loop will there be for the call fib(6)?

A) 6
B) 5
C) 4
D) 3
Question
Consider the recursive version of the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
How many recursive calls to fib(2) will be made from the original call of fib(6)?

A) 2
B) 3
C) 4
D) 5
Question
Consider the recursive square method shown below that takes a non-negative int argument.
Public int square(int n)
{
Return square(n, n);
}
Public int square(int c, int n)
{
If (c == 1)
{
Return n;
}
Else
{
Return n + square(c - 1, n);
}
}
Assume that the last return statement is changed to this:
Return n * square(c - 1, n);
What would a call to square(4) return?

A) 4
B) 16
C) 64
D) 256
Question
Consider the fib method from the textbook shown below.
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
Calling fib(3) will trigger ___ recursive call(s) and execute the terminating condition ___ time(s), respectively.

A) 1, 1
B) 2, 2
C) 1, 2
D) 2, 1
Question
Consider the recursive square method shown below that takes a non-negative int argument:
Public int square(int n)
{
Return square(n, n);
}
Public int square(int c, int n)
{
If (c == 1)
{
Return n;
}
Else
{
Return n + square(c - 1, n);
}
}
Assume that the last return statement is changed to this:
Return square(c - 1, n);
What would a call to square(7) return?

A) 7
B) 13
C) 14
D) 49
Question
What is the purpose of a recursive helper method?

A) Shield the user of the recursive method from the recursive details.
B) Speed up the execution.
C) Eliminate the recursion.
D) Add another base case.
Question
Consider the recursive version of the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
How many more recursive calls to fib will be made from the original call of fib(7) than from the original call of fib(6) (not counting the original calls)?

A) 1
B) 2
C) 5
D) 10
Question
Consider the recursive version of the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
How many total recursive calls (not counting the original call) to fib will be made from the original call of fib(7)?

A) 12
B) 24
C) 30
D) 32
Question
Consider the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1; // line #1
}
Else
{
Return fib(n - 1) + fib(n - 2); // line #2
}
}
Assume line #1 is changed to this:
If (n <= 2) { return 2; }
How will this change affect the result of calling fib(7)?

A) It will add 2 to the return from fib(7)
B) It will add 4 to the return from fib(7)
C) It will add 8 to the return from fib(7)
D) It will double the return from fib(7)
Question
Consider the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
Computing the 7th fibonacci number, fib(7), recursively computes fib(6), fib(5), and fib(4) ___ times respectively.

A) 1, 2, and 3
B) 6, 5, and 4
C) 4, 5, and 6
D) 3, 2, and 1
Question
Consider the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1; // line #1
}
Else
{
Return fib(n - 1) + fib(n - 2); // line #2
}
}
Assume line #2 is changed to this:
Else { return 2 * fib(n - 1) + 2 * fib(n - 2); }
What effect will this change have?

A) This will return 5 from fib(4)
B) This will return 8 from fib(4)
C) This will return 10 from fib(4)
D) This will cause an exception on a call fib(4)
Question
A palindrome is a word or phrase that reads the same forward or backward. Consider the following code snippet:
Public boolean palindrome(String string)
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
What is the purpose of the palindrome method?

A) Return the palindrome to the calling method.
B) Provide the string, along with its first and last indexes to the recursive isPal method.
C) Send the recursive isPal method its terminating condition.
D) Recursively call itself.
Question
Consider the recursive version of the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
How many total recursive calls (not counting the original call) to fib will be made from the original call of fib(6)?

A) 6
B) 12
C) 14
D) 20
Question
A palindrome is a word or phrase that reads the same forward or backward. Consider the following code snippet:
Public boolean palindrome(String string)
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
What does the condition left >= right refer to?

A) An empty or one-character string is considered a palindrome.
B) The string is not a palindrome.
C) It cannot be determined if the string is a palindrome.
D) You have reached the middle of the string.
Question
Why does the best recursive method usually run slightly slower than its iterative counterpart?

A) Testing the terminating condition takes longer.
B) Each recursive method call takes processor time.
C) Multiple recursive cases must be considered.
D) Checking multiple terminating conditions take more processor time.
Question
Consider the recursive square method shown below. It takes a non-negative int argument. Then it recursively adds the number n to itself n times to produce the square of n. Complete the correct code for the square helper method.
Public int square(int n)
{
____________________;
}
Public int square(int c, int n)
{
If (c == 1)
{
Return n;
}
Else
{
Return n + square(c - 1, n);
}
}

A) return square(n, n)
B) return square(n)
C) return square(n - 1, n)
D) return square(n, n - 1)
Question
A palindrome is a word or phrase spelled which reads the same forward or backward. Consider the following code snippet:
Public boolean palindrome(String string)
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
What does the method palindrome return?

A) true
B) false
C) a palindrome not found exception
D) the Boolean value returned from the isPal method
Question
A palindrome is a word or phrase that reads the same forward or backward. Consider the methods palindrome and isPal shown below:
Public boolean palindrome(String string)
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
The method palindrome as shown here would be considered to be a ____ method.

A) recursive
B) terminating
C) helper
D) static
Question
Consider the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1; // line #1
}
Else
{
Return fib(n - 1) + fib(n - 2); // line #2
}
}
Assume line #1 is changed to this:
If (n <= 2) { return n; }
What effect will this change have?

A) This will return 21 from fib(6)
B) This will return 13 from fib(6)
C) This will return 8 from fib(6)
D) This will return 6 from fib(6)
Question
Suppose we wrote a new version of fib called newFib. What does the call newFib(6) return?
Public static long newFib(int n)
{
If (n <= 3)
{
Return 1;
}
Else
{
Return newFib(n - 1) + newFib(n - 2) + newFib(n - 3);
}
}

A) 3
B) 5
C) 7
D) 9
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/100
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 13: Recursion
1
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)?

A) 1
B) 5
C) 6
D) 11
B
2
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
{
______________________________
}
}

A) return (printSum(n - 1));
B) return (n + printSum(n + 1));
C) return (n + printSum(n - 1));
D) return (n - printSum(n - 1));
C
3
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?

A) Prints a positive int value forward, digit by digit.
B) Prints a positive int value backward, digit by digit.
C) Divides the int by 10 and prints out its last digit.
D) Divides the int by 10 and prints out the result.
B
4
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?

A) line #1
B) line #2
C) line #3
D) line #4
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
5
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)?

A) 3
B) 6
C) 18
D) 729
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
6
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?

A) Those with width equal to 0.
B) Those with width equal to 1.
C) Those with width greater than or equal to 2.
D) Triangles of any width.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
7
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)?

A) 0
B) -10
C) -22
D) Nothing - a StackoverflowError exception will occur
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
8
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?

A) A negative or zero width would cause problems.
B) Nothing - the method would still be correct.
C) We would lose our only recursive case.
D) A positive width would reduce the correct area by 1.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
9
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

A) I
B) II
C) I and II
D) I, II, and III
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
10
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.

A) n < 1
B) n <= 1
C) fib(n - 1)
D) fib(n - 1) + fib(n - 1)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
11
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)?

A) 1
B) -1
C) 120
D) 840
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
12
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)?

A) line #1
B) line #2
C) lines #1 and #2
D) line #4
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
13
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)?

A) 10
B) 8
C) 4
D) 21
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
14
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?

A) n <= 0
B) n == 1
C) n <= 0 or n == 1
D) n > 0
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
15
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)?

A) line #1
B) line #2
C) lines #1 and #2
D) line #4
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
16
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?

A) if (n == -1)
B) if (n <= 0)
C) if (n >= 0)
D) The terminating case as shown will avoid infinite recursion.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
17
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)?

A) 821
B) 128
C) 12
D) 10
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
18
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)?

A) 1
B) 2
C) 6
D) 24
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
19
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.

A) if (i == 0)
B) return j
C) return add(i - 1, j + 1)
D) there is no terminating condition
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
20
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?

A) The method will still return correct results for all non-negative width triangles.
B) The method will return incorrect results for triangles with width equal to 0.
C) The method will return incorrect results for triangles with width equal to 1.
D) The method will return area to be one too high for all triangles.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
21
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?

A) n == 0
B) n == 1
C) all n with n < 0
D) all n with n >= 0
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
22
Which of the following options could be used as a terminating condition for a recursive method that finds the middle character of a String with any number of characters?
I the length of the String is 1
II first and last String characters match
III the String is not empty

A) I
B) II
C) I, II and III
D) I and III
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
23
Consider the method powerOfTwo shown below:
Public boolean powerOfTwo(int n)
{
If (n == 1) // line #1
{
Return true;
}
Else if (n % 2 == 1) // line #2
{
Return false;
}
Else
{
Return powerOfTwo(n / 2); // line #3
}
}
How many recursive calls are made from the original call of powerOfTwo(64) (not including the original call)?

A) 8
B) 6
C) 4
D) 2
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
24
If a recursive method does not simplify the computation within the method and the base case is not called, what will be the result?

A) The terminating condition will be executed and recursion will end.
B) The recursion calculation will occur correctly regardless.
C) This cannot be determined.
D) Infinite recursion will occur.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
25
When a recursive method is called, and it does not perform recursion, what must be true?

A) The terminating condition was true.
B) One recursive case condition was true.
C) All recursive case conditions were true.
D) An exception will occur in the method.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
26
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)
{
_____________________________
{
Return 1;
}
Else
{
Return(anInteger * myFactorial(anInteger - 1));
}
}

A) if (anInteger == 1)
B) if ((anInteger - 1) == 1)
C) if (anInteger * (anInteger - 1) == 1)
D) if (myFactorial(anInteger) == 1)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
27
Consider the getArea method from the textbook shown below:
Public int getArea()
{
If (width <= 0) { return 0; } // line #1
Triangle smallerTriangle = new Triangle(width - 1); // line #2
Int smallerArea = smallerTriangle.getArea(); // line #3
Return smallerArea + width; // line #4
}
If line#1 was removed, what would be the result?

A) The recursive method would cause an exception for values below 0.
B) The recursive method would construct triangles whose width was negative.
C) The recursive method would terminate when the width reached 0.
D) The recursive method would correctly calculate the area of the original triangle.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
28
Consider the method powerOfTwo shown below:
Public boolean powerOfTwo(int n)
{
If (n == 1) // line #1
{
Return true;
}
Else if (n % 2 == 1) // line #2
{
Return false;
}
Else
{
Return powerOfTwo(n / 2); // line #3
}
}
How many recursive calls are made from the original call powerOfTwo(63) (not including the original call)?

A) 6
B) 4
C) 1
D) 0
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
29
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);
}
}

A) return 1;
B) return x;
C) return power(x, n - 1);
D) return x * power(x, n - 1);
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
30
Consider the method powerOfTwo shown below:
Public boolean powerOfTwo(int n)
{
If (n == 1) // line #1
{
Return true;
}
Else if (n % 2 == 1) // line #2
{
Return false;
}
Else
{
Return powerOfTwo(n / 2); // line #3
}
}
What is the best interpretation of line #1?

A) One is a power of two
B) One is not a power of two
C) Any multiple of one is a power of two
D) 1 is an invalid choice for n
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
31
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
}
If line #1 was eliminated from the method, what would be the result when calling getArea?

A) A negative or zero width would cause problems.
B) Nothing - the method would still work correctly.
C) We would lose our only recursive case.
D) A positive width would reduce the correct area by 1.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
32
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));
}
}

A) 1
B) 2
C) 4
D) 8
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
33
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
}
Assume line #3 is changed to this:
Triangle smallerTriangle = new Triangle(width + 1)
When calling the getArea method on a Triangle object with width = 4, what result will be produced?

A) area for all triangles will be computed to be too high
B) area for all triangles will be computed to be too low
C) area will only be incorrect for a triangle objects with width = 1
D) infinite recursion will occur for triangle objects with width >= 2
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
34
____ recursion can occur when a recursive algorithm does not contain a special case to handle the simplest computations directly.

A) Mutual
B) Non-mutual
C) Terminating condition
D) Infinite
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
35
A recursive method without a special terminating case would _________

A) end immediately.
B) not be recursive.
C) be more efficient.
D) never terminate.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
36
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
}
Assume that line #2 is changed to this:
If (width == 1) { return 2; }
How would this affect calls to getArea?

A) It would add 1 to all calls except those where width <= 0.
B) It would make no difference to any call.
C) It would double every triangle area.
D) It would subtract 1 from all calls.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
37
If recursion does not have a special terminating case, what error will occur?

A) Index out of range
B) Illegal argument
C) Stack overflow
D) Out of memory
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
38
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
}
Assume that line #3 is changed to this:
Triangle smallerTriangle = new Triangle(width);
This would cause infinite recursion for ____.

A) triangles with width equal to 0
B) triangles with width equal to 1
C) triangles with width greater than or equal to 2
D) triangles of any width
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
39
Would switching the special case order affect the return value of the following method?
Public int mystery(int n, int m)
{
If (n == 0) // special case #1
{
Return 0;
}
If (n == 1) // special case #2
{
Return m;
}
Return m + mystery(n - 1, m);
}

A) No
B) Yes
C) It is impossible to tell.
D) An exception will be thrown.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
40
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
{
______________________
}
}

A) return(anInteger * (myFactorial(anInteger)));
B) return ((anInteger - 1) * (myFactorial(anInteger)));
C) return(anInteger * (myFactorial(anInteger - 1)));
D) return((anInteger - 1)*(myFactorial(anInteger - 1)));
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
41
Given the following class code:
Public class RecurseMore
{
Private static int total;
Public static void main(String[] args)
{
System.out.println(recurse(4));
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;
}
Else
{
Total = 4 + recurse(n - 2);
}
Return total;
}
}
What values will be printed when this code is executed?

A) 0, 4, and 8
B) 4 and 8
C) 4
D) 8
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
42
Complete the following code snippet, which is intended to be a recursive method that will find the smallest value in an array of double values from index to the end of the array:
Public static double minVal(double[] elements, int index)
{
If (index == elements.length - 1)
{
Return elements[index];
}
Double val = __________________;
If (elements[index] < val)
{
Return elements[index];
}
Else
{
Return val;
}
}

A) minVal(elements, index - 1)
B) minVal(index - 1)
C) minVal(elements, index + 1)
D) minVal(index + 1)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
43
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)
{
Answer = 1;
}
Else
{
_______________________________________
}
Return answer;
}

A) answer = baseNum * calcPower (baseNum -1, exponent);
B) answer = baseNum * calcPower (baseNum, exponent - 1);
C) answer = baseNum * calcPower (baseNum, exponent);
D) answer = baseNum * calcPower (baseNum -1, exponent - 1);
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
44
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;
}

A) answer = 0;
B) answer = 1;
C) answer = -1;
D) answer = calcPower(1);
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
45
Complete the following code snippet, which is intended to be a recursive method that reverses a String value:
Public static String reverseIt(String s)
{
If
{
Return s;
}
Else
{
________________________
}
}

A) return reverseIt;
B) return reverseIt;
C) return reverseIt;
D) return reverseIt;
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
46
Given the following code snippet:
Public static int newCalc(int n)
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (1 + newCalc(n / 10));
}
}
What value will be returned when this code is executed with a call to newCalc(15)?

A) 2
B) 2.5
C) 5
D) 5.5
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
47
Given the following code snippet:
Public static int newCalc(int n)
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (n % 10) + newCalc(n / 10);
}
}
What value will be returned when this code is executed with a call to newCalc(5)?

A) 2
B) 2.5
C) 5
D) 5.5
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
48
Complete the following code snippet, which is intended to be a recursive method that will find the smallest value in an array of double values from index to the end of the array:
Public static double minVal(double[] elements, int index)
{
If (index == elements.length - 1)
{
__________________
}
Double val = minVal(elements, index + 1);
If (elements[index] < val)
{
Return elements[index];
}
Else
{
Return val;
}
}

A) return elements[index];
B) return 1;
C) return 0;
D) return elements[0];
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
49
Given the following class code:
Public class RecurseSample
{
Public static void main(String[] args)
{
System.out.println(recurse(3));
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;
}
Else
{
Total = 3 + recurse(n - 1);
}
Return total;
}
}
What values will be printed when this code is executed?

A) 6
B) 9
C) 3, 6, and 9
D) 1, 3, 6, and 9
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
50
Complete the following code snippet, which is intended to be a recursive method that reverses a String value:
Public static String reverseIt(String s)
{
If
{
_________
}
Else
{
Return reverseIt;
}
}

A) return s;
B) return 0;
C) return s.charAt(0);
D) return s.substring(1);
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
51
Given the following code snippet:
Public static int newCalc(int n)
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (1 + newCalc(n / 10));
}
}
What value will be returned when this code is executed with a call to newCalc(5)?

A) 1
B) 1.5
C) 5
D) 5.5
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
52
Complete the following code snippet, which is intended to be a recursive method that will find the sum of all elements in an array of double values from index to the end of the array:
// return the sum of all elements in arr[]
Public static double findSum(double arr[], int index)
{
If (index == 0)
{
_____________________
}
Else
{
Return (arr[index] + findSum(arr, index - 1));
}
}
Assume that this method would be called using an existing array named myArray as follows:
FindSum(myArray,myArray.length - 1);

A) return arr[index];
B) return arr[index + 1];
C) return arr[1];
D) return arr[index - 1];
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
53
Given the following class code:
Public class RecurseSample
{
Public static void main(String[] args)
{
Recurse(3);
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;}
Else
{
Total = 3 + recurse(n - 1);
}
System.out.println(total);
Return total;
}
}
What values will be printed?

A) 1, 3, and 6
B) 1, 3, 6, and 9
C) 3, 6, and 9
D) 3, 6, 9, and 12
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
54
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)
{
______________________
}
Else
{
Return (anInteger * myFactorial(anInteger - 1));
}
}

A) return 0;
B) return -anInteger;
C) return 1;
D) return myFactorial(anInteger);
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
55
Complete the following code snippet, which is intended to be a recursive method that will find the sum of all elements in an array of double values from index to the end of the array:
// return the sum of all elements in arr[]
Public static double findSum(double arr[], int index)
{
If (index == 0)
{
Return arr[index];
}
Else
{
_____________________
}
}
Assume that this method would be called using an existing array named myArray as follows:
FindSum(myArray, myArray.length - 1);

A) return (findSum(arr, index + 1));
B) return (findSum(arr, index - 1));
C) return (arr[index] + findSum(arr, index + 1));
D) return (arr[index] + findSum(arr, index - 1));
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
56
Complete the code for the recursive method shown below, which is intended to compute the sum of the first n integers:
Public int s(int n)
{
If (n == 1)
{
Return 1;
}
Else
{
_________________
}
}

A) return n + (n - 1);
B) return s(n) + n - 1;
C) return n + s(n - 1);
D) return n + s(n + 1);
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
57
Given the following class code:
Public class RecurseMore
{
Public static void main(String[] args)
{
Recurse(4);
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;
}
Else
{
Total = 4 + recurse(n - 2);
}
System.out.println(total);
Return total;
}
}
What values will be printed when this code is executed?

A) 0, 4, and 8
B) 4 and 8
C) 4
D) 8
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
58
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 #
}
Else
{
Return (n + printSum(n)); //line #3
}
}
Which of the following statements is correct?

A) line #1 is incorrect, and should be changed to if (n <= 1)
B) line #3 is incorrect, and should be changed to return (printSum (n - 1));
C) line #3 is incorrect, and should be changed to return (n + printSum (n + 1));
D) line #3 is incorrect, and should be changed to return (n + printSum (n - 1));
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
59
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;
________________________
{
Answer = 1;
}
Else
{
Answer = baseNum * calcPower (baseNum, exponent - 1);
}
Return answer;
}

A) if (exponent == 0)
B) if (exponent == 1)
C) if (exponent == -1)
D) if (exponent != 1)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
60
Given the following code snippet:
Public static int newCalc(int n)
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (n % 10) + newCalc(n / 10);
}
}
What value will be returned when this code is executed with a call to newCalc(15)?

A) 2
B) 2.5
C) 6
D) 6.5
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
61
Consider the iterative version of the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Long fold = 1;
Long fold2 = 1;
Long fnew = 1;
For (int i = 3; i <= n; i++)
{
Fnew = fold + fold2;
Fold2 = fold;
Fold = fnew;
}
Return fnew;
}
How many iterations of the for loop will there be for the call fib(6)?

A) 6
B) 5
C) 4
D) 3
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
62
Consider the recursive version of the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
How many recursive calls to fib(2) will be made from the original call of fib(6)?

A) 2
B) 3
C) 4
D) 5
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
63
Consider the recursive square method shown below that takes a non-negative int argument.
Public int square(int n)
{
Return square(n, n);
}
Public int square(int c, int n)
{
If (c == 1)
{
Return n;
}
Else
{
Return n + square(c - 1, n);
}
}
Assume that the last return statement is changed to this:
Return n * square(c - 1, n);
What would a call to square(4) return?

A) 4
B) 16
C) 64
D) 256
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
64
Consider the fib method from the textbook shown below.
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
Calling fib(3) will trigger ___ recursive call(s) and execute the terminating condition ___ time(s), respectively.

A) 1, 1
B) 2, 2
C) 1, 2
D) 2, 1
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
65
Consider the recursive square method shown below that takes a non-negative int argument:
Public int square(int n)
{
Return square(n, n);
}
Public int square(int c, int n)
{
If (c == 1)
{
Return n;
}
Else
{
Return n + square(c - 1, n);
}
}
Assume that the last return statement is changed to this:
Return square(c - 1, n);
What would a call to square(7) return?

A) 7
B) 13
C) 14
D) 49
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
66
What is the purpose of a recursive helper method?

A) Shield the user of the recursive method from the recursive details.
B) Speed up the execution.
C) Eliminate the recursion.
D) Add another base case.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
67
Consider the recursive version of the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
How many more recursive calls to fib will be made from the original call of fib(7) than from the original call of fib(6) (not counting the original calls)?

A) 1
B) 2
C) 5
D) 10
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
68
Consider the recursive version of the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
How many total recursive calls (not counting the original call) to fib will be made from the original call of fib(7)?

A) 12
B) 24
C) 30
D) 32
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
69
Consider the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1; // line #1
}
Else
{
Return fib(n - 1) + fib(n - 2); // line #2
}
}
Assume line #1 is changed to this:
If (n <= 2) { return 2; }
How will this change affect the result of calling fib(7)?

A) It will add 2 to the return from fib(7)
B) It will add 4 to the return from fib(7)
C) It will add 8 to the return from fib(7)
D) It will double the return from fib(7)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
70
Consider the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
Computing the 7th fibonacci number, fib(7), recursively computes fib(6), fib(5), and fib(4) ___ times respectively.

A) 1, 2, and 3
B) 6, 5, and 4
C) 4, 5, and 6
D) 3, 2, and 1
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
71
Consider the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1; // line #1
}
Else
{
Return fib(n - 1) + fib(n - 2); // line #2
}
}
Assume line #2 is changed to this:
Else { return 2 * fib(n - 1) + 2 * fib(n - 2); }
What effect will this change have?

A) This will return 5 from fib(4)
B) This will return 8 from fib(4)
C) This will return 10 from fib(4)
D) This will cause an exception on a call fib(4)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
72
A palindrome is a word or phrase that reads the same forward or backward. Consider the following code snippet:
Public boolean palindrome(String string)
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
What is the purpose of the palindrome method?

A) Return the palindrome to the calling method.
B) Provide the string, along with its first and last indexes to the recursive isPal method.
C) Send the recursive isPal method its terminating condition.
D) Recursively call itself.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
73
Consider the recursive version of the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
How many total recursive calls (not counting the original call) to fib will be made from the original call of fib(6)?

A) 6
B) 12
C) 14
D) 20
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
74
A palindrome is a word or phrase that reads the same forward or backward. Consider the following code snippet:
Public boolean palindrome(String string)
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
What does the condition left >= right refer to?

A) An empty or one-character string is considered a palindrome.
B) The string is not a palindrome.
C) It cannot be determined if the string is a palindrome.
D) You have reached the middle of the string.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
75
Why does the best recursive method usually run slightly slower than its iterative counterpart?

A) Testing the terminating condition takes longer.
B) Each recursive method call takes processor time.
C) Multiple recursive cases must be considered.
D) Checking multiple terminating conditions take more processor time.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
76
Consider the recursive square method shown below. It takes a non-negative int argument. Then it recursively adds the number n to itself n times to produce the square of n. Complete the correct code for the square helper method.
Public int square(int n)
{
____________________;
}
Public int square(int c, int n)
{
If (c == 1)
{
Return n;
}
Else
{
Return n + square(c - 1, n);
}
}

A) return square(n, n)
B) return square(n)
C) return square(n - 1, n)
D) return square(n, n - 1)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
77
A palindrome is a word or phrase spelled which reads the same forward or backward. Consider the following code snippet:
Public boolean palindrome(String string)
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
What does the method palindrome return?

A) true
B) false
C) a palindrome not found exception
D) the Boolean value returned from the isPal method
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
78
A palindrome is a word or phrase that reads the same forward or backward. Consider the methods palindrome and isPal shown below:
Public boolean palindrome(String string)
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
The method palindrome as shown here would be considered to be a ____ method.

A) recursive
B) terminating
C) helper
D) static
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
79
Consider the fib method from the textbook shown below:
Public static long fib(int n)
{
If (n <= 2)
{
Return 1; // line #1
}
Else
{
Return fib(n - 1) + fib(n - 2); // line #2
}
}
Assume line #1 is changed to this:
If (n <= 2) { return n; }
What effect will this change have?

A) This will return 21 from fib(6)
B) This will return 13 from fib(6)
C) This will return 8 from fib(6)
D) This will return 6 from fib(6)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
80
Suppose we wrote a new version of fib called newFib. What does the call newFib(6) return?
Public static long newFib(int n)
{
If (n <= 3)
{
Return 1;
}
Else
{
Return newFib(n - 1) + newFib(n - 2) + newFib(n - 3);
}
}

A) 3
B) 5
C) 7
D) 9
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 100 flashcards in this deck.