Lesson 4.16: Recursion
Lesson 4.16: Recursion
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:
- Write out each call as an indented line.
- Find the base case — that's where the unwinding starts.
- Work back up the chain, substituting return values.
- 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
mystery(4)?
public static int mystery(int n) {
if (n == 0) return 0;
return n + mystery(n - 1);
}
public static int power(int base, int exp) {
if (exp == 0) return 1;
return base * power(base, exp - 1);
}
return base * power(base, exp - 1)
if (exp == 0) return 1
exp - 1
power(2, 3) using the method above?StackOverflowError occurs at runtime.public static void count(int n) {
if (n == 0) return;
System.out.print(n + " ");
count(n - 1);
}
count(4);
Mastery: Recursion
public static void countUp(int n) {
if (n == 0) return;
countUp(n - 1);
System.out.print(n + " ");
}
countUp(4);
mystery(12)?
public static int mystery(int n) {
if (n < 10) return n;
return n % 10 + mystery(n / 10);
}
public static int sumDown(int n) {
if (n == 0) return 1;
return n + sumDown(n - 1);
}
n + 1 instead of n - 1.f(5)?
public static int f(int n) {
if (n <= 1) return 1;
return f(n - 1) + f(n - 2);
}
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);
}
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]