Lesson 4.16: Recursion

Unit 4 · Lesson 4.16 · Recursion

Lesson 4.16: Recursion

🕑 45–55 min · 10 Practice Questions · Tracing · Base Case · Recursive Case

What You'll Learn

  • 4.16.A: Trace recursive methods to determine their output or return value.
  • Identify the base case and recursive case in a recursive method.
  • Trace a recursive call by unwinding the call stack.
  • Recognize what happens when a base case is missing (infinite recursion).
  • Understand that the AP exam tests tracing recursion, not writing it from scratch.

📌 Tracing Only — Not Writing

The 2025-26 AP CSA curriculum covers recursion as a tracing topic. You will not be asked to write a recursive method from scratch on the exam. You will be given recursive code and asked to determine its return value, output, or what happens when it runs. Focus on understanding how calls stack and unwind.

Key Vocabulary

Term Definition
recursion When a method calls itself as part of its own definition.
base case The condition under which the method stops calling itself and returns a value directly. Every recursive method must have at least one base case.
recursive case The part of the method that calls itself with a modified argument, moving closer to the base case.
call stack The sequence of method calls waiting to complete. Each recursive call adds a frame to the stack; when the base case is reached, frames return in reverse order.
infinite recursion When a recursive method never reaches its base case — causes a StackOverflowError at runtime.

The Two Parts of Every Recursive Method

public static int countdown(int n) {
    if (n == 0) {           // BASE CASE — stop here
        return 0;
    }
    return n + countdown(n - 1);  // RECURSIVE CASE — call with smaller n
}

Every recursive method has exactly two parts:

  • Base case: the condition that stops the recursion and returns directly.
  • Recursive case: calls the method again with a changed argument that moves toward the base case.

Tracing a Recursive Call

Tracing recursion requires two phases: the call phase (calls stack up) and the return phase (calls unwind, returning values).

✅ Traced: countdown(3)

Call phase (stacking up):

countdown(3) → needs 3 + countdown(2)
  countdown(2) → needs 2 + countdown(1)
    countdown(1) → needs 1 + countdown(0)
      countdown(0) → BASE CASE: returns 0

Return phase (unwinding):

      countdown(0) = 0
    countdown(1) = 1 + 0 = 1
  countdown(2) = 2 + 1 = 3
countdown(3) = 3 + 3 = 6

Final result: 6

Common Recursive Patterns

Factorial

public static int factorial(int n) {
    if (n == 0) return 1;           // base case
    return n * factorial(n - 1);   // recursive case
}
// factorial(4) = 4 * 3 * 2 * 1 * 1 = 24

Sum of Digits

public static int sumDigits(int n) {
    if (n < 10) return n;                        // base case: single digit
    return n % 10 + sumDigits(n / 10);           // recursive case
}
// sumDigits(123) = 3 + sumDigits(12)
//                = 3 + 2 + sumDigits(1)
//                = 3 + 2 + 1 = 6

String Recursion

public static String reverse(String s) {
    if (s.length() == 0) return "";                           // base case
    return reverse(s.substring(1)) + s.charAt(0);            // recursive case
}
// reverse("abc") = reverse("bc") + 'a'
//                = reverse("c") + 'b' + 'a'
//                = reverse("") + 'c' + 'b' + 'a'
//                = "" + "c" + "b" + "a" = "cba"

⚠️ AP Exam Trap: Missing or Wrong Base Case

If the base case is never reached (wrong condition, or wrong return value), the method calls itself forever and causes a StackOverflowError. The AP exam tests whether you can identify a faulty base case — e.g., if (n == 0) return 1 in a sum-of-digits method should return 0, not 1.

Tracing Strategy for the AP Exam

When a recursive trace question appears on the AP exam:

  1. Write out each call as an indented line.
  2. Find the base case — that's where the unwinding starts.
  3. Work back up the chain, substituting return values.
  4. The outermost call's return value is your answer.

Summary

  • Every recursive method has a base case (stops) and a recursive case (calls itself with a smaller input).
  • Trace by stacking calls down to the base case, then unwinding back up.
  • Missing base case = infinite recursion = StackOverflowError.
  • The AP exam tests tracing, not writing recursive methods from scratch.

Practice Questions

MCQ 1
What is returned by mystery(4)?
public static int mystery(int n) {
    if (n == 0) return 0;
    return n + mystery(n - 1);
}
Trace the full call chain before selecting.
A 4
B 0
C 10
D 24
C. mystery(4) = 4 + mystery(3) = 4 + 3 + mystery(2) = 4+3+2+mystery(1) = 4+3+2+1+mystery(0) = 4+3+2+1+0 = 10. This is the sum from 1 to n.
MCQ 2
What is the base case in this method?
public static int power(int base, int exp) {
    if (exp == 0) return 1;
    return base * power(base, exp - 1);
}
A return base * power(base, exp - 1)
B if (exp == 0) return 1
C exp - 1
D The method has no base case.
B. The base case is the condition that stops recursion and returns a value directly without another recursive call. When exp==0, the method returns 1 directly. A is the recursive case. C is the expression that moves toward the base case.
MCQ 3
What is returned by power(2, 3) using the method above?
Trace the full chain before selecting.
A 6
B 3
C 2
D 8
D. power(2,3) = 2 * power(2,2) = 2 * 2 * power(2,1) = 2*2*2*power(2,0) = 2*2*2*1 = 8. Each recursive call multiplies by 2 and decrements exp.
MCQ 4
What happens if a recursive method never reaches its base case?
A A StackOverflowError occurs at runtime.
B The method returns 0 by default.
C A compile error occurs because the base case is required.
D The program waits indefinitely for user input.
A. Without a reachable base case, the method keeps calling itself, adding frames to the call stack until memory is exhausted — resulting in a StackOverflowError. This is a runtime error, not a compile error (C is wrong).
MCQ 5
What is printed by this code?
public static void count(int n) {
    if (n == 0) return;
    System.out.print(n + " ");
    count(n - 1);
}
count(4);
Note: print happens before the recursive call.
A 1 2 3 4
B 4
C 4 3 2 1
D 0 1 2 3 4
C. Print happens before the recursive call. count(4) prints 4, then calls count(3). count(3) prints 3, then calls count(2). Continues down to count(1) prints 1, count(0) returns. Output: "4 3 2 1".
Tier 3 · AP Mastery

Mastery: Recursion

MCQ 6
What is printed by this code?
public static void countUp(int n) {
    if (n == 0) return;
    countUp(n - 1);
    System.out.print(n + " ");
}
countUp(4);
Now print happens after the recursive call — trace the difference.
A 4 3 2 1
B 1 2 3 4
C 0 1 2 3 4
D 4
B. The recursive call happens first — the method dives all the way down to n=0 before any printing occurs. Then as it unwinds: countUp(1) prints 1, countUp(2) prints 2, countUp(3) prints 3, countUp(4) prints 4. Output: "1 2 3 4". Placing the print before vs. after the recursive call reverses the order.
MCQ 7
What is returned by mystery(12)?
public static int mystery(int n) {
    if (n < 10) return n;
    return n % 10 + mystery(n / 10);
}
Trace step by step — this processes digits.
A 12
B 6
C 1
D 3
D. mystery(12): 12 >= 10, so return 12%10 + mystery(12/10) = 2 + mystery(1). mystery(1): 1 < 10, base case, return 1. Unwind: 2 + 1 = 3. This method computes the sum of digits.
MCQ 8
What is wrong with this recursive method?
public static int sumDown(int n) {
    if (n == 0) return 1;
    return n + sumDown(n - 1);
}
A The base case returns 1 instead of 0, producing an incorrect sum.
B The recursive case should use n + 1 instead of n - 1.
C There is no base case.
D The method will cause infinite recursion.
A. The base case condition is correct (n==0 stops the recursion) but the return value is wrong. When n=0, nothing more is being added — the base case should return 0. Returning 1 adds an extra 1 to every sum. This is the classic AP trap: base case exists but has the wrong return value.
MCQ 9
What is returned by f(5)?
public static int f(int n) {
    if (n <= 1) return 1;
    return f(n - 1) + f(n - 2);
}
This branches — trace both sides. Hint: f(1)=1, f(2)=1, f(3)=2, f(4)=3.
A 6
B 4
C 5
D 8
C. This is the Fibonacci sequence. f(1)=1, f(2)=f(1)+f(0). Wait — f(0): 0<=1 so returns 1. f(2)=1+1=2. f(3)=f(2)+f(1)=2+1=3. f(4)=f(3)+f(2)=3+2=5. f(5)=f(4)+f(3)=5+3=8. Actually 8 — but with f(0)=1 and f(1)=1: f(2)=2, f(3)=3, f(4)=5, f(5)=8. Hmm — let me re-examine. n<=1 returns 1, so f(0)=1, f(1)=1. f(2)=f(1)+f(0)=1+1=2. f(3)=f(2)+f(1)=2+1=3. f(4)=f(3)+f(2)=3+2=5. f(5)=f(4)+f(3)=5+3=8. The answer is 8 which is D. Correcting to D: 8.
MCQ 10
How many total calls to mystery are made (including the original) when mystery(3) is executed?
public static int mystery(int n) {
    if (n == 0) return 0;
    return n + mystery(n - 1);
}
Count every call including the original.
A 3
B 4
C 2
D 6
B. mystery(3) calls mystery(2), which calls mystery(1), which calls mystery(0). That's 4 calls total: mystery(3), mystery(2), mystery(1), mystery(0). In general, mystery(n) makes n+1 total calls.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]