Recursion Practice Questions

AP (Advanced Placement) · AP Computer Science A · 150 free MCQs with instant results and detailed explanations.

150
Total
49
Easy
75
Medium
26
Hard

Start Practicing Recursion

Take a timed quiz or customize your practice session

Quick Quiz (10 Qs) → Mock Test (25 Qs) ⚙ Customize

Sample Questions from Recursion

Here are 10 sample questions. Start a quiz to get randomized questions with scoring.

Q1
Easy
What is the base case in a recursive function?
A. The condition under which the function stops calling itself.
B. The part of the function that performs the recursive call.
C. The output of the function when it is called without any arguments.
D. The return value of the function when it is called with the maximum input.
Show Answer & Explanation
Correct Answer: A
The base case is crucial in recursion because it prevents infinite loops by providing a condition where the recursion stops. Without a base case, a recursive function would continue calling itself indefinitely.
Q2
Easy
What will the output of the following code be? public static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } System.out.println(factorial(5));
A. 5
B. 15
C. 120
D. 60
Show Answer & Explanation
Correct Answer: C
The factorial function multiplies the number n by the factorial of (n-1) until it reaches the base case of 0, where it returns 1. Therefore, factorial(5) = 5 * 4 * 3 * 2 * 1 = 120.
Q3
Easy
Which of the following scenarios is best suited for a recursive solution?
A. Calculating the nth Fibonacci number.
B. Sorting a large dataset.
C. Counting the number of elements in an array.
D. Finding the maximum value in a list.
Show Answer & Explanation
Correct Answer: A
Calculating Fibonacci numbers is a classic example of a problem that can be elegantly solved using recursion, as it involves breaking down the problem into smaller subproblems of the same type.
Q4
Medium
What will be the output of the following recursive method when called with input 5? ``` public int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); } ```
A. 120
B. 60
C. 5
D. 1
Show Answer & Explanation
Correct Answer: A
The factorial of 5 is 5 * 4 * 3 * 2 * 1, which equals 120. The function correctly implements the factorial calculation using recursion.
Q5
Medium
Consider the following recursive function: ``` public void printNumbers(int n) { if (n > 0) { printNumbers(n - 1); System.out.println(n); } } ``` What will be the output of `printNumbers(3)`?
A. 3 2 1
B. 1 2 3
C. 2 1 3
D. 3 1 2
Show Answer & Explanation
Correct Answer: B
The function decrements n until it reaches 0, then prints numbers from 1 to 3 in ascending order, as the print statement is called after the recursive call.
Q6
Medium
What is the time complexity of the following recursive function that computes the Fibonacci sequence? ``` public int fibonacci(int n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); } ```
A. O(n)
B. O(n^2)
C. O(2^n)
D. O(n log n)
Show Answer & Explanation
Correct Answer: C
The Fibonacci function has an exponential time complexity of O(2^n) due to the repeated calculations of values, leading to an exponential growth in function calls.
Q7
Medium
Which of the following correctly describes the base case in a recursive algorithm?
A. The condition that stops the recursion.
B. The initial input to the algorithm.
C. The maximum depth of recursion.
D. The last value computed by the recursive function.
Show Answer & Explanation
Correct Answer: A
The base case is crucial in recursion as it defines when the recursion should stop, preventing infinite recursion and ensuring proper execution of the algorithm.
Q8
Hard
Consider the following recursive function: ```java public static int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); } ``` What is the time complexity of this function?
A. O(1)
B. O(n)
C. O(n log n)
D. O(n^2)
Show Answer & Explanation
Correct Answer: B
The time complexity is O(n) because the function calls itself n times, reducing n by 1 with each call, until it reaches the base case.
Q9
Hard
Given the following recursive method, what will be the return value of compute(4)? ```java public static int compute(int x) { if (x == 0) return 1; return 2 * compute(x - 1) + 3; } ```
A. 11
B. 19
C. 27
D. 31
Show Answer & Explanation
Correct Answer: C
The function computes a value recursively. For compute(4), it expands as follows: compute(4) = 2 * compute(3) + 3 = 2 * (2 * compute(2) + 3) + 3 = 2 * (2 * (2 * compute(1) + 3) + 3) + 3 = 2 * (2 * (2 * (2 * compute(0) + 3) + 3) + 3) + 3). This simplifies down to compute(4) = 2 * (2 * (2 * (2 * 1 + 3) + 3) + 3) = 27.
Q10
Hard
What is the output of the following recursive method if the input is 5? public int recursiveMethod(int n) { if (n == 0) return 1; return n * recursiveMethod(n - 1); }
A. 120
B. 60
C. 24
D. 5
Show Answer & Explanation
Correct Answer: A
The method calculates the factorial of the input number. For n=5, it computes 5! = 5 * 4 * 3 * 2 * 1 = 120.

Showing 10 of 150 questions. Start a quiz to practice all questions with scoring and timer.

Practice All 150 Questions →

Recursion โ€” AP (Advanced Placement) AP Computer Science A Practice Questions Online

This page contains 150 practice MCQs for the chapter Recursion in AP (Advanced Placement) AP Computer Science A. The questions are organized by difficulty โ€” 49 easy, 75 medium, 26 hard โ€” so you can choose the right level for your preparation.

Every question includes a detailed explanation to help you understand the concept, not just memorize answers. Take a timed quiz to simulate exam conditions, or practice at your own pace with no time limit.