APCS Chapter 13 Test Review

1) 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

c) I and II

2) 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 smallerAr

d) line #4

3) 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 smallerAr

c) lines #1 and #2

7) 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

a) if (i == 0)

8) 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

b) n <= 1

9) 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)

c) n <= 0 or n == 1

10) 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) 5

11) 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

c) 18

12) 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

b) 8

13) 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)

b) 128

15) 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) retur

c) return (n + printSum(n - 1));

16) 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

d) Nothing - a StackoverflowError exception will occur

17) 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 shoul

b) if (n <= 0)

19) 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

c) 120

20) 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

b) 2

21) 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,

a) return 1;

22) 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

c) Stack overflow

23) A recursive method without a special terminating case would _________
a) end immediately.
b) not be recursive.
c) be more efficient.
d) never terminate.

d) never terminate.

24) 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 followin

c) all n with n < 0

25) ____ 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

d) Infinite

28) 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 met

a) The terminating condition was true.

33) 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

d) 8

34) 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

a) No

35) 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

a) I

37) Consider the method below, which implements the exponentiation operation recursively. Select the statement that should be used to complete the method, so that it handles the special case correctly.
public static double power(int base, int exponent)
{

a) return 1;

38) Consider the method below, which displays the characters from a String in reverse order. Each character appears on a separate line. Select the statement that should be used to complete the method, so that it performs a recursive method call correctly.

c) printReverse(word.substring(1));

39) Consider the method below, which prints the digits of an arbitrary integer in reverse order, one digit per line. The method should print the last digit first. Then, it should recursively print the integer obtained by removing the last digit. Select th

b)
System.out.println(value % 10);
printReverse(value / 10);

40) 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

a) One is a power of two

41) 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

d) 0

42) 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

b) 6

43) 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
{
______________________
}

c) return(anInteger * (myFactorial(anInteger - 1)));

44) 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(anInteg

a) if (anInteger == 1)

45) 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 (anI

c) return 1;

46) 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

c) return n + s(n - 1);

47) 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;

a) if (exponent == 0)

48) 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;

b) answer = 1;

49) 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;

b) answer = baseNum * calcPower (baseNum, exponent - 1);

50) 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(tota

c) 3, 6, and 9

51) 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);
}
re

b) 9

52) 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

b) 4 and 8

53) 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(1

c) 6

54) 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

c) 5

55) 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)

c) 5

56) 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

a) 2

57) 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 =

d) 8

58) 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 ==

c) minVal(elements, index + 1)

59) 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 ==

a) return elements[index];

60) 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 fin

d) return (arr[index] + findSum(arr, index - 1));