Recursion
Help Questions
AP Computer Science A › Recursion
For factorial(5) below, how many recursive calls are made before the base case returns?
int factorial(int n) {
if (n <= 1) return 1; // base case
return n * factorial(n-1); // recursive case
}
```
4 recursive calls are made
The recursion never terminates
5 recursive calls are made
6 recursive calls are made
Explanation
This question tests AP Computer Science A skills in understanding recursion, specifically tracing recursive calls and identifying when the base case is reached. Recursion involves solving a problem by solving smaller instances of the same problem, using base cases to terminate the recursion and recursive cases to reduce the problem size. In this problem, the recursive function factorial(n) uses the base case (n <= 1) returning 1, and the recursive case calling factorial(n-1). Choice A is correct because factorial(5) calls factorial(4), then factorial(3), then factorial(2), then factorial(1), which hits the base case - that's exactly 4 recursive calls before the base case returns. Choice D is incorrect because the function does terminate when n reaches 1. To help students: Teach them to trace recursive calls step-by-step, drawing a call tree or stack diagram. Emphasize counting only the recursive calls, not the initial call, and practice identifying when the base case is reached.
What is the output of the recursive function for input $n = 6$ in the Fibonacci code?
int fib(int n) {
if (n <= 1) return n; // base cases
return fib(n-1) + fib(n-2); // recursive case
}
```
It returns 13
It returns 8
It never reaches a base case
It returns 6
Explanation
This question tests AP Computer Science A skills in understanding recursion, specifically computing Fibonacci numbers using recursive implementation. Recursion involves solving a problem by solving smaller instances of the same problem, using base cases to terminate the recursion and recursive cases to reduce the problem size. In this problem, the recursive function fib(n) uses base cases (n <= 1) returning n, and the recursive case returning fib(n-1) + fib(n-2). Choice B is correct because fib(6) = fib(5) + fib(4) = 5 + 3 = 8, following the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8. Choice C is incorrect because it represents fib(7) rather than fib(6). To help students: Teach them to trace Fibonacci recursion carefully, noting that fib(0)=0 and fib(1)=1 by the base case. Use memoization or dynamic programming to show the inefficiency of naive recursion, and practice building up the sequence from the base cases.
How many recursive calls are made when evaluating fib(4) using the function below?
int fib(int n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
}
```
9 recursive calls are made
5 recursive calls are made
4 recursive calls are made
7 recursive calls are made
Explanation
This question tests AP Computer Science A skills in understanding recursion, specifically counting recursive calls in tree-like recursive structures. Recursion involves solving a problem by solving smaller instances of the same problem, using base cases to terminate the recursion and recursive cases to reduce the problem size. In this problem, the recursive function fib(n) makes two recursive calls for each non-base case, creating a binary tree of calls. Choice B is correct because fib(4) makes 7 total recursive calls: fib(4) calls fib(3) and fib(2); fib(3) calls fib(2) and fib(1); the first fib(2) calls fib(1) and fib(0); the second fib(2) also calls fib(1) and fib(0) - counting all recursive calls gives 7. Choice C is incorrect because it overcounts the calls. To help students: Draw the complete call tree for small examples, labeling each node with its argument. Teach them to count all nodes except the root, and emphasize how exponential growth occurs in naive Fibonacci recursion.
Which statement correctly describes the base case and recursive case in this binary search?
int bSearch(int[] a, int target, int lo, int hi) {
if (lo > hi) return -1; // base case
int mid = (lo + hi) / 2;
if (a<u>mid</u> == target) return mid; // base case
if (target < a<u>mid</u>) return bSearch(a, target, lo, mid-1);
return bSearch(a, target, mid+1, hi);
}
```
Base: lo>hi or found; recurse on half-interval
Base: lo==hi; recurse by scanning neighbors
Base: mid==0; recurse by decrementing target
No base case exists, so recursion is infinite
Explanation
This question tests AP Computer Science A skills in understanding recursion, specifically identifying base and recursive cases in binary search implementation. Recursion involves solving a problem by solving smaller instances of the same problem, using base cases to terminate the recursion and recursive cases to reduce the problem size. In this problem, the recursive binary search has two base cases: when lo > hi (search space exhausted) returning -1, and when a[mid] == target (element found) returning mid; the recursive cases search either the left half (lo to mid-1) or right half (mid+1 to hi). Choice B is correct because it accurately identifies both base cases (lo>hi or element found) and describes the recursive case as operating on half-intervals. Choice A is incorrect because lo==hi is not the base case - the search continues even when lo equals hi. To help students: Emphasize that recursive algorithms can have multiple base cases. Teach them to identify all termination conditions and trace through examples to see how the search space shrinks by half each time.
If a = <u>2,4,6,8,10</u>, what will bSearch(a, 8, 0, 4) return?
int bSearch(int[] a, int target, int lo, int hi) {
if (lo > hi) return -1;
int mid = (lo + hi) / 2;
if (a<u>mid</u> == target) return mid;
if (target < a<u>mid</u>) return bSearch(a, target, lo, mid-1);
return bSearch(a, target, mid+1, hi);
}
```
It returns index 3
It returns -1
It returns index 2
It returns index 4
Explanation
This question tests AP Computer Science A skills in understanding recursion, specifically tracing binary search execution on a concrete example. Recursion involves solving a problem by solving smaller instances of the same problem, using base cases to terminate the recursion and recursive cases to reduce the problem size. In this problem, the recursive binary search looks for target 8 in array [2,4,6,8,10] with initial bounds 0 to 4. Choice B is correct because: first call has mid=(0+4)/2=2, a[2]=6<8, so recurse on (3,4); second call has mid=(3+4)/2=3, a[3]=8==8, so return 3. Choice A is incorrect because index 2 contains value 6, not 8. To help students: Practice tracing binary search step-by-step, calculating mid values using integer division. Emphasize array indexing starts at 0, and teach them to verify their answer by checking that a[returned_index] equals the target value.
How many recursive calls are made by hanoi(3, "A", "B", "C") before all moves print?
void hanoi(int n, String from, String aux, String to) {
if (n == 1) { print(from + "->" + to); return; } // base case
hanoi(n-1, from, to, aux); // recursive case
print(from + "->" + to);
hanoi(n-1, aux, from, to);
}
```
7 calls are made total
15 calls are made total
It never terminates for n=3
3 calls are made total
Explanation
This question tests AP Computer Science A skills in understanding recursion, specifically counting recursive calls in the Tower of Hanoi problem. Recursion involves solving a problem by solving smaller instances of the same problem, using base cases to terminate the recursion and recursive cases to reduce the problem size. In this problem, the recursive hanoi function makes two recursive calls for each n>1, creating a binary tree structure of calls. Choice B is correct because hanoi(3) makes exactly 7 total recursive calls: the initial call makes 2 recursive calls with n=2, each of those makes 2 recursive calls with n=1, giving 1 + 2 + 4 = 7 total calls. Choice C is incorrect because it counts something other than recursive calls (possibly move operations). To help students: Draw the complete recursion tree for small n values. Teach the pattern that hanoi(n) makes $2^n$ - 1 total calls, and practice distinguishing between counting recursive calls versus counting print statements.
How many recursive calls are made by isPal("abba") before reaching the base case?
boolean isPal(String s) {
if (s.length() <= 1) return true;
if (s.charAt(0) != s.charAt(s.length()-1)) return false;
return isPal(s.substring(1, s.length()-1));
}
```
1 recursive call is made
2 recursive calls are made
3 recursive calls are made
4 recursive calls are made
Explanation
This question tests AP Computer Science A skills in understanding recursion, specifically counting recursive calls in palindrome checking. Recursion involves solving a problem by solving smaller instances of the same problem, using base cases to terminate the recursion and recursive cases to reduce the problem size. In this problem, the recursive isPal function removes matching characters from both ends until reaching the base case. Choice B is correct because isPal("abba") makes exactly 2 recursive calls: first call checks "abba" and recurses with "bb", second call checks "bb" and recurses with "" (empty string), which hits the base case (length <= 1). Choice C is incorrect because it counts the initial call or misunderstands when the base case is reached. To help students: Trace the shrinking string at each recursive call, showing "abba" → "bb" → "". Emphasize that we count only the recursive calls, not the initial call, and teach them to identify when the base case condition is satisfied.
What is the output of isPal("racecar") using the recursive palindrome checker below?
boolean isPal(String s) {
if (s.length() <= 1) return true; // base case
if (s.charAt(0) != s.charAt(s.length()-1)) return false;
return isPal(s.substring(1, s.length()-1)); // recursive case
}
```
It causes infinite recursion
It returns true
It returns "racecar"
It returns false
Explanation
This question tests AP Computer Science A skills in understanding recursion, specifically evaluating palindrome checking with recursive string manipulation. Recursion involves solving a problem by solving smaller instances of the same problem, using base cases to terminate the recursion and recursive cases to reduce the problem size. In this problem, the recursive isPal function checks if first and last characters match, then recursively checks the substring without those characters. Choice B is correct because "racecar" is a palindrome: 'r'=='r', then "aceca": 'a'=='a', then "cec": 'c'=='c', then "e" has length 1 (base case), returning true all the way back up. Choice A is incorrect because the function correctly identifies "racecar" as a palindrome. To help students: Trace through palindrome checking step-by-step, showing how the string shrinks. Emphasize the importance of the base case for strings of length 0 or 1, and practice with both palindromes and non-palindromes.