Chapter11 TB
Chapter11 TB
Chapter 11
Recursion
◼ Multiple Choice
1) A recursive method is one that:
(a) Returns a value
(b) Initializes a set of variables
(c) Returns no value
(d) Invokes itself
Answer: D
4) Recursion is:
(a) the ability of a program to repeat a block of code
(b) the ability of a method to call itself
(c) the ability of a method to call smaller methods
(d) the ability of a method to implement factorials
Answer: B
Chapter 11 Recursion 2
6) Infinite recursion:
(a) will happen when there is no base case
(b) will not happen when there is a base case
(c) will not happen if we use subproblems
(d) none of the above
Answer: A
10) During recursion, if the stack attempts to grow beyond its limit, a _____________ occurs.
(a) Stack underflow
(b) Stack overflow
(c) Recursive underflow
(d) Recursive overflow
Answer: B
Chapter 11 Recursion 3
13) The following code for the method factorial() applies to the next two questions:
if (n == 0)
return 1;
else
return n * factorial(n-1);
What is the value returned when the integer 3 is the argument to the factorial method?
(a) 2
(b) 4
(c) 6
(d) 8
Answer: C
Chapter 11 Recursion 4
14) What is the value returned when the integer 5 is the argument to the factorial method?
(a) 15
(b) 50
(c) 100
(d) 120
Answer: D
◼ True/False
1) A method definition that includes a call to itself is said to be recursive.
Answer: True
2) When a recursive call is encountered, computation is temporarily suspended; all of the information
needed to continue the computation is saved and the recursive call is evaluated.
Answer: True
7) A recursively written method will usually run slower and use more storage than an equivalent
iterative version.
Answer: True
10) The binary search algorithm is extremely slow compared to an algorithm that simply tries all array
elements in order.
Answer: False
11) The binary search algorithm has worst-case running time that is logarithmic.
Answer: True
◼ Short Answer/Essay
1) Write a recursive method to compute the power of xn for non-negative n.
Answer:
if(n < 0)
System.exit(0);
}
Chapter 11 Recursion 6
if(n > 0)
else
int result = 1;
if(n < 0)
System.exit(0);
result = result * x;
return result;
}
Chapter 11 Recursion 7
if(size >0)
System.out.print(s.substring(size-1, size));
writeBackwards(s, size-1);
System.out.print(s.charAt(i));
if(n < 0)
{
System.out.println(“Sorry negative numbers not allowed.”);
System.exit(0);
}
return(1);
else
return (n * factorial(n-1));
if(n < 0)
{
System.out.println(“Sorry negative numbers not allowed.”);
System.exit(0);
}
int result = 1;
result = result * i;
Chapter 11 Recursion 9
return result;
12) What are the criteria you must consider when formulating a recursive solution?
Answer: When formulating a recursive solution, you should ask yourself:
How can the problem be defined as a smaller problem of the same type?
How does each recursive call diminish the size of the problem?
What is the base case?
As the problem size diminishes, is the base case ever reached?
14) What are the two base cases for a recursive binary search algorithm?
Answer: One base case for the recursive binary search algorithm is the key not found case in which
the index of the first element of the array is greater than the index of the last element of the array.
The second base case for the recursive binary search algorithm is when the key is found, which is
represented by the key being equal to the value in the array at the current midpoint.
15) What are two factors that contribute to the inefficiency of some recursive solutions?
Answer: One factor that contributes to the inefficiency of some recursive solutions is the overhead
associated with the recursive call. The other factor is the inherent inefficiency of some recursive
algorithms.
Chapter 11 Recursion 10