Recursion is when a function calls itself to solve smaller parts of a problem until it reaches a simple case it can solve directly.
Click to reveal answer
beginner
What is a base case in recursion?
A base case is the simplest situation in a recursive function that stops the recursion by not calling itself again.
Click to reveal answer
intermediate
How does the call stack help in recursion?
The call stack keeps track of all the active function calls. Each recursive call adds a new layer, and when a base case is reached, the stack unwinds as functions return their results.
Click to reveal answer
intermediate
What happens if a recursive function has no base case?
Without a base case, the recursive function keeps calling itself forever, causing a stack overflow error because the call stack grows too large.
Click to reveal answer
beginner
Explain the flow of a simple factorial function using recursion.
The factorial function calls itself with smaller numbers until it reaches 1 (base case). Then it returns 1, and each previous call multiplies its number by the returned value, building the final result.
Click to reveal answer
What is the purpose of the base case in a recursive function?
ATo increase the size of the problem
BTo call the function again with the same input
CTo stop the recursion and prevent infinite calls
DTo print the result
✗ Incorrect
The base case stops the recursion by providing a simple answer without further calls.
What does the call stack do during recursion?
ARuns functions in parallel
BDeletes previous function calls immediately
CPrevents recursion from happening
DStores all active function calls until they finish
✗ Incorrect
The call stack keeps track of each recursive call until the base case is reached and functions return.
What error occurs if a recursive function never reaches a base case?
AStack overflow
BSyntax error
CType error
DMemory leak
✗ Incorrect
Without a base case, the call stack grows until it runs out of space, causing a stack overflow.
In the factorial example, what does factorial(3) call next?
Afactorial(2)
Bfactorial(4)
Cfactorial(1)
Dfactorial(0)
✗ Incorrect
factorial(3) calls factorial(2) to solve a smaller problem.
Which of these is NOT true about recursion?
AEvery recursive function must have a base case
BRecursion always uses loops internally
CRecursion breaks a problem into smaller parts
DThe call stack grows with each recursive call
✗ Incorrect
Recursion does not require loops; it uses function calls to repeat.
Describe how the call stack changes during a recursive function call.
Think about how each call waits for the next before finishing.
You got /4 concepts.
Explain why a base case is essential in recursion and what happens if it is missing.
Consider what stops the function from calling itself forever.
You got /4 concepts.
Practice
(1/5)
1. What happens when a recursive function calls itself without a base case?
easy
A. The function converts into a loop automatically.
B. It causes infinite recursion and eventually a stack overflow error.
C. The function runs only once and stops.
D. The function returns immediately without doing anything.
Solution
Step 1: Understand the role of base case in recursion
A base case stops the recursion by providing a condition to end the repeated calls.
Step 2: Analyze what happens without a base case
Without a base case, the function keeps calling itself endlessly, filling the call stack until it overflows.
Final Answer:
It causes infinite recursion and eventually a stack overflow error. -> Option B
Quick Check:
No base case = infinite recursion [OK]
Hint: Always check for a base case to stop recursion [OK]
Common Mistakes:
Thinking recursion stops automatically without base case
Confusing recursion with loops
Assuming function returns without base case
2. Which of the following is the correct syntax to define a recursive function in C that calculates factorial?
easy
A. int factorial(int n) { if (n == 1) return 0; else return n + factorial(n - 1); }
B. int factorial(int n) { while (n > 0) return n * factorial(n - 1); }
C. int factorial(int n) { if (n == 0) return 0; else return n * factorial(n + 1); }
D. int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); }
Solution
Step 1: Check base case correctness
Factorial base case is when n == 0, return 1.
Step 2: Check recursive call and operation
Recursive call should be factorial(n - 1) multiplied by n.
Final Answer:
int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } -> Option D
Quick Check:
Base case n==0 and multiply with factorial(n-1) [OK]
Hint: Base case returns 1; recursive call uses n-1 [OK]