AP CSA Recursion 1 — Questions and Answers
Question 1: What is the base case in a recursive method?
- The condition that stops the recursion (Correct answer)
- The first recursive call
- The return type of the method
- The method signature
Correct answer: The condition that stops the recursion
The base case is the condition that terminates recursion by returning a result without making further recursive calls.
Question 2: What error occurs when a recursive method has no base case or never reaches it?
- StackOverflowError (Correct answer)
- NullPointerException
- ArrayIndexOutOfBoundsException
- RecursionLimitException
Correct answer: StackOverflowError
Infinite recursion causes the call stack to fill up, resulting in a StackOverflowError at runtime.
Question 3: What does the following return? `public int f(int n) { if(n==0) return 0; return n + f(n-1); }` called with f(4)?
- 10 (Correct answer)
- 4
- 0
- 24
Correct answer: 10
f(4) = 4 + f(3) = 4+3+2+1+0 = 10, computing the sum of integers from 0 to n.
Question 4: In recursion, each method call gets its own set of local variables stored where?
- On the call stack (Correct answer)
- In the heap
- In a global variable table
- In a static memory block
Correct answer: On the call stack
Each recursive call creates a new stack frame on the call stack that holds its own local variables and parameters.
Question 5: Which of the following best describes mutual recursion?
- Two methods that each call the other (Correct answer)
- A method calling itself twice
- A loop that calls a recursive method
- A method with two base cases
Correct answer: Two methods that each call the other
Mutual recursion occurs when method A calls method B and method B calls method A, forming a cycle.
Question 6: What is the recursive case?
- The part of the method that makes a call to itself with a smaller/simpler input (Correct answer)
- The stopping condition
- The method's return statement
- The parameter declaration
Correct answer: The part of the method that makes a call to itself with a smaller/simpler input
The recursive case is the branch that calls the method again with a modified argument, moving toward the base case.
What is the base case in a recursive method?