Chaper 16
Given the following code that uses recursion to find the factorial of a number, how many times will the else clause be executed if n = 5? private static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); }
5
A method is called from the main method for the first time. It then calls itself seven times. What is the depth of recursion?
7
In the following code that uses recursion to find the greatest common divisor of a number, what is the base case? public static int gcd(int x, int y) { if (x % y == 0) return y; else return gcd(y, x % y); }
if (x % y == 0) return y;
The actions performed by the JVM that take place with each method call are sometimes referred to as ________.
overhead
A method that calls itself is a ________ method.
recursive
In the ________, the problem must be reduced to a smaller version of the original problem.
recursive case
A recursive method must have which of the following? a control variable initialized to a starting value All of these some way to control the number of time it repeats a statement that increments the control variable
some way to control the number of time it repeats
Which of the following problems can be solved recursively? Towers of Hanoi greatest common denominator binary search All of these.
All of these.
________ occurs when method A calls method B which in turn calls method A.
Indirect recusion
The ________ is at least one case in which a problem can be solved without recursion.
base case
In a recursive program, the number of times a method calls itself is known as the ________.
depth of recursion
When recursive methods directly call themselves, it is known as ________.
direct recursion
To use recursion for a binary search, what is required before the search can proceed?
The array must be sorted.
In the following code that uses recursion to find the factorial of a number, what is the base case? private static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); }
if (n == 0) return 1;