Question 121
What is the return value of f(5)?
public static int f(int n) {
if (n <= 0) return 0;
if (n == 1) return 1;
return f(n - 1) + f(n - 2) + 1;
}Main Solution
We trace the recursive calls step by step, using the definition:
f(0) = 0f(1) = 1f(n) = f(n-1) + f(n-2) + 1forn > 1
Compute each value:
| n | f(n) |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | f(1) + f(0) + 1 = 1 + 0 + 1 = 2 |
| 3 | f(2) + f(1) + 1 = 2 + 1 + 1 = 4 |
| 4 | f(3) + f(2) + 1 = 4 + 2 + 1 = 7 |
| 5 | f(4) + f(3) + 1 = 7 + 4 + 1 = 12 |
Therefore, f(5) = 12, which is answer choice A.
Teacher Explanation
This question tests students' ability to trace a recursive method that is similar to the Fibonacci sequence but with an extra +1 added at each recursive step. Students must carefully apply the base cases (n <= 0 returns 0, n == 1 returns 1) and build up from the bottom. The +1 in the recursive case is the key distinguishing feature from standard Fibonacci.
Education Coach Advice
AP Exam Strategy: When tracing recursive methods, always build a table from the base cases upward. Write out each value of f(0), f(1), f(2), ... systematically rather than trying to trace the full call tree top-down. Pay close attention to any extra terms (like the +1 here) that modify a familiar pattern such as Fibonacci — these are intentional traps designed to catch students who rely on memorized sequences.
Traps
Common traps:
- Option C (7): Students who forget the
+1in the recursive case compute standard Fibonacci numbers: f(5) = 5, or they may stop at f(4) = 7, confusing f(4) with f(5). - Option D (11): Students may make an arithmetic error, such as computing f(4) = 6 instead of 7, leading to f(5) = 6 + 4 + 1 = 11.
- Option B (15): Students may add an extra
+1at the top level or miscount the recursive additions.
Answer images

