Recursion

AP Computer Science A · Unit 4: Data structures

Recursion notes

Fact-checked by a second, independent AI review.

Recursion in AP CSA uses a base case to stop, and a recursive case to reduce the problem toward that base case. Typical questions ask you to write methods like factorial, sum of an array, or binary search, with attention to base case, recursive step, and stack depth.

Base case
The simplest input for which the method returns a result without making another recursive call.
Recursive case
The part that calls the method again with a smaller or simpler problem to solve it step by step toward the base case.
Call stack / depth
Each recursive call adds a stack frame; depth determines space complexity and can cause stack overflow if too deep.
Convergence
Ensure every path leads to the base case; no infinite recursion by verifying decreasing problem size or proper termination.
Typical exam questions
Write recursive methods for factorial, sum of array elements, binary search, or string reversal; identify base and recursive parts and state time/space complexity.

Remember it: BASE case, RECURSE, RETURN: use this three-step recipe every time you code a recursion.

Exam tip: Markers look for a correct base case, a properly reducing recursive step, no extra work after recursive calls, and clear explanations of time and space complexity including call depth. Be explicit about what each call does and why it terminates.

Syllabus checklist

  • Recursion: core ideas
  • Terms and typical exam questions for "Data structures" in AP Computer Science A (Java
  • College Board)

Practice questions

  1. 1. What is the base case in a recursive method?

    Answer checked

    Show answer

    Answer: The case that stops recursion. Base case stops further recursive calls when the problem is solved.

  2. 2. Recursion depth can exceed memory if there is no base case.

    Answer checked

    Show answer

    Answer: True. Without a base case, calls continue until stack overflow occurs.

  3. 3. Naive Fibonacci shows exponential ____ due to repeated calls.

    Answer checked

    Show answer

    Answer: time. Repeated calls create many branches, causing exponential time growth.

  4. 4. Which is true about recursion vs iteration?

    Answer checked

    Show answer

    Answer: Recursion uses a stack to store frames, while iteration does not. Recursion relies on the call stack; iteration uses loop constructs instead.

  5. 5. What is the number of recursive calls in factorial(3) including the initial call?

    Answer checked

    Show answer

    Answer: 4. Factorial(3) -> factorial(2) -> factorial(1) -> factorial(0): 4 calls total.

Practise recursion as a game

Hearts, combos, streaks and an AI tutor that explains every answer. Start free.

Start free

More from Unit 4: Data structures