PrepGo

AP Computer Science A Practice Quiz: Recursion

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 10 questions to check your progress.

Question 1 of 10

According to the provided definition, which two components are essential for a properly functioning recursive method?

All Questions (10)

According to the provided definition, which two components are essential for a properly functioning recursive method?

A) A for-loop and a while-loop

B) At least one base case and at least one recursive call

C) A parameter and a return value

D) A local variable and a call to a different method

Correct Answer: B

The content explicitly states that 'Recursive methods contain at least one base case, which halts the recursion, and at least one recursive call.' These are the fundamental requirements for recursion.

The provided content states that 'Recursion is another form of repetition' and that any recursive solution can be replicated through an iterative approach. This means that recursion is functionally equivalent to which programming construct?

A) An if-else statement

B) A switch statement

C) A loop

D) Variable assignment

Correct Answer: C

Iteration is achieved through loops (like for or while loops). The content directly links recursion to repetition and iteration, making it functionally equivalent to a loop.

Consider the following recursive method: ```java public static void countDown(int n) { if (n > 0) { System.out.print(n + " "); countDown(n - 1); } } ``` What is the output when `countDown(4)` is called?

A) 1 2 3 4

B) 4 3 2 1

C) 4

D) 1

Correct Answer: B

The method first prints the current value of `n` and then calls itself with `n-1`. The call `countDown(4)` prints '4 ', then calls `countDown(3)`. `countDown(3)` prints '3 ', then calls `countDown(2)`. This continues until `countDown(1)` prints '1 ' and calls `countDown(0)`, which hits the base case (`n > 0` is false) and stops the recursion.

Based on the provided content, what is the role of parameters in a recursive method?

A) They are shared and have the same value across all recursive calls.

B) They are used only in the base case to halt the recursion.

C) They capture the progress of the recursion, similar to a loop control variable.

D) They must always be integers.

Correct Answer: C

The content states, 'Parameter values capture the progress of a recursive process, much like loop control variable values capture the progress of a loop.' Each call has its own set of parameters, tracking its specific state.

Consider the following recursive method: ```java public static int mystery(int k) { if (k <= 0) { return 0; } return k + mystery(k - 2); } ``` What is the result of the call `mystery(5)`?

A) 5

B) 6

C) 9

D) 15

Correct Answer: C

The execution trace is as follows: - `mystery(5)` returns `5 + mystery(3)` - `mystery(3)` returns `3 + mystery(1)` - `mystery(1)` returns `1 + mystery(-1)` - `mystery(-1)` returns `0` (base case) Substituting back: `1 + 0 = 1`. Then `3 + 1 = 4`. Then `5 + 4 = 9`.

A programmer writes the following method, intending for it to run recursively: ```java public static void infinite(int val) { System.out.println(val); infinite(val + 1); } ``` What is the most likely result of calling `infinite(1)`?

A) The method will print `1` and then terminate successfully.

B) The method will not compile because it has no return statement.

C) The method will cause a runtime error because it lacks a base case to halt recursion.

D) The method will print numbers indefinitely without error.

Correct Answer: C

The method continuously calls itself with an increasing value (`val + 1`), but there is no condition to stop the calls. This is a missing base case. The program will run out of memory for the call stack and produce a runtime error (typically a StackOverflowError).

Consider the following recursive method: ```java public static String reverse(String str) { if (str.isEmpty()) { return str; } return reverse(str.substring(1)) + str.charAt(0); } ``` What is the result of the call `reverse("AP")`?

A) "AP"

B) "PA"

C) "A"

D) "P"

Correct Answer: B

The execution trace is: - `reverse("AP")` calls `reverse("P")` and waits. It will append 'A' to the result. - `reverse("P")` calls `reverse("")` and waits. It will append 'P' to the result. - `reverse("")` hits the base case and returns "". - The `reverse("P")` call receives "" and returns "" + 'P', which is "P". - The `reverse("AP")` call receives "P" and returns "P" + 'A', which is "PA".

The content states that 'Each recursive call has its own set of local variables, including the parameters.' What is the primary implication of this statement?

A) A change to a parameter in one recursive call will affect the parameter's value in all other calls.

B) The state of each step in the recursion is stored independently, preventing interference between calls.

C) Recursive methods cannot use local variables, only parameters.

D) Recursive methods are less memory-efficient than iterative solutions because they cannot have variables.

Correct Answer: B

Because each call has its own memory space for its parameters and local variables (its own 'frame' on the call stack), the state of one call is isolated from others. This allows the recursion to work its way back up, with each returning call having preserved its unique state.

A programmer successfully implements an algorithm using an iterative approach with a `for` loop. According to the provided content, what can be concluded about this algorithm?

A) It is impossible to implement the same algorithm using recursion.

B) The iterative approach is the only possible way to solve the problem.

C) An equivalent solution can be implemented using a recursive approach.

D) The recursive approach would be significantly more complex.

Correct Answer: C

The content explicitly states, 'Any recursive solution can be replicated through the use of an iterative approach and vice versa.' This means the two approaches are interchangeable for any given problem.

Consider the following recursive method, which is a variation of the Fibonacci sequence: ```java public static int combo(int n) { if (n <= 1) { return 1; } return combo(n - 1) + combo(n - 2); } ``` What is the result of the call `combo(4)`?

A) 3

B) 4

C) 5

D) 8

Correct Answer: C

This requires tracing multiple recursive branches: - `combo(4)` calls `combo(3)` + `combo(2)` - `combo(3)` calls `combo(2)` + `combo(1)`. `combo(1)` returns 1. - The first `combo(2)` calls `combo(1)` + `combo(0)`. Both return 1. So `combo(2)` returns 1 + 1 = 2. - So, `combo(3)` returns 2 + 1 = 3. - The second `combo(2)` (from the original call) also returns 2. - Finally, `combo(4)` returns the result of `combo(3)` + `combo(2)`, which is 3 + 2 = 5.