The base case is the condition that stops the recursion. It prevents infinite calls by providing a simple answer without further recursion.
Click to reveal answer
beginner
What is the recursive case in recursion?
The recursive case is the part where the function calls itself with a smaller or simpler input, moving towards the base case.
Click to reveal answer
beginner
Why is the base case important in a recursive function?
Without a base case, the recursive function would call itself forever, causing a stack overflow error.
Click to reveal answer
intermediate
In the factorial function, what is the base case and what is the recursive case?
Base case: factorial(0) = 1 (stop condition). Recursive case: factorial(n) = n * factorial(n-1) (calls itself with smaller n).
Click to reveal answer
beginner
How does the recursive case help solve a problem step-by-step?
It breaks the problem into smaller parts by calling itself with simpler inputs, gradually reaching the base case to stop.
Click to reveal answer
What happens if a recursive function has no base case?
AIt only runs once
BIt returns immediately
CIt never calls itself
DIt runs forever and causes an error
✗ Incorrect
Without a base case, the function keeps calling itself endlessly, causing a stack overflow.
Which part of a recursive function calls itself?
ARecursive case
BBase case
CMain function
DReturn statement
✗ Incorrect
The recursive case is where the function calls itself with a smaller input.
In the factorial example, what is factorial(0)?
A1
B0
CUndefined
DFactorial of 1
✗ Incorrect
Factorial of 0 is defined as 1, which is the base case.
What does the base case do in recursion?
ACalls the function again
BStarts the recursion
CStops the recursion
DIncreases the input size
✗ Incorrect
The base case stops the recursion by providing a simple answer.
Which of these is a correct recursive case for sum(n) = 1 + 2 + ... + n?
Asum(n) = sum(n+1) + n
Bsum(n) = sum(n-1) + n
Csum(n) = n
Dsum(n) = 0
✗ Incorrect
The recursive case calls sum with n-1 and adds n to it.
Explain the difference between the base case and the recursive case in recursion.
Think about what stops the recursion and what continues it.
You got /3 concepts.
Describe how a recursive function solves a problem step-by-step using base and recursive cases.
Imagine climbing down stairs one step at a time until you reach the bottom.
You got /3 concepts.
Practice
(1/5)
1. In recursion, what is the purpose of the base case?
easy
A. To print the result of the recursion
B. To call the function again with a smaller input
C. To increase the input size for the next call
D. To stop the recursion and prevent infinite calls
Solution
Step 1: Understand the base case role
The base case is the condition where recursion stops to avoid infinite calls.
Step 2: Differentiate base case from recursive case
The recursive case calls the function again, but the base case ends recursion.
Final Answer:
To stop the recursion and prevent infinite calls -> Option D
Quick Check:
Base case stops recursion [OK]
Hint: Base case stops recursion to avoid infinite loops [OK]
Common Mistakes:
Confusing base case with recursive case
Thinking base case calls function again
Ignoring base case leads to infinite recursion
2. Which of the following is the correct syntax for a base case in a recursive Java method that calculates factorial?
easy
A. if (n > 0) return n * factorial(n - 1);
B. return n * factorial(n + 1);
C. if (n == 0) return 1;
D. if (n == 1) factorial(n);
Solution
Step 1: Identify base case condition for factorial
Factorial base case is when n equals 0, return 1.
Step 2: Check syntax correctness
if (n == 0) return 1; correctly returns 1 when n == 0, stopping recursion.
Final Answer:
if (n == 0) return 1; -> Option C
Quick Check:
Base case for factorial is n == 0 [OK]
Hint: Base case returns fixed value, usually simplest input [OK]
Common Mistakes:
Using wrong condition for base case
Calling factorial with n + 1 causing infinite recursion
Not returning a value in base case
3. What is the output of this Java code?
public class Test {
public static int sum(int n) {
if (n == 0) return 0;
return n + sum(n - 1);
}
public static void main(String[] args) {
System.out.println(sum(3));
}
}