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 the base case in a recursive function?
The base case is the simplest situation where the function stops calling itself to avoid infinite loops.
Click to reveal answer
intermediate
How does the call stack help in recursion?
The call stack keeps track of all the function calls. Each recursive call adds a new layer, and when a base case is reached, the stack unwinds to return results.
Click to reveal answer
intermediate
What happens if a recursive function has no base case?
Without a base case, the function keeps calling itself forever, causing a stack overflow error because the call stack grows too large.
Click to reveal answer
advanced
Explain the difference between direct and indirect recursion.
Direct recursion is when a function calls itself. Indirect recursion happens when a function calls another function that eventually calls the first function again.
Click to reveal answer
What is the purpose of the base case in recursion?
ATo increase the call stack size
BTo make the function call itself more times
CTo print the result
DTo stop the recursion and prevent infinite calls
✗ Incorrect
The base case stops recursion by providing a simple answer, preventing infinite calls.
What does the call stack do during recursion?
AKeeps track of all active function calls
BDeletes previous function calls
CRuns functions in parallel
DPrevents recursion
✗ Incorrect
The call stack stores each function call until it finishes, helping manage recursion.
What error occurs if recursion has no base case?
AMemory leak
BSyntax error
CStack overflow
DType error
✗ Incorrect
Without a base case, recursive calls never stop, causing a stack overflow error.
Which of these is an example of direct recursion?
AFunction A calls Function B, which calls Function C
BFunction A calls itself
CFunction A calls Function B, which calls Function A
DFunction A calls Function B only once
✗ Incorrect
Direct recursion is when a function calls itself directly.
In recursion, what happens when the base case is reached?
AThe function stops calling itself and returns a value
BThe function calls itself again
CThe call stack resets
DThe program crashes
✗ Incorrect
Reaching the base case stops recursion and starts returning values back through the call stack.
Describe how the call stack works during a recursive function call.
Think about how each call waits for the next before finishing.
You got /3 concepts.
Explain why a base case is essential in recursion and what could happen without it.
Consider what happens if the function never stops calling itself.
You got /3 concepts.
Practice
(1/5)
1. What happens when a recursive function calls itself without a base case?
easy
A. It returns immediately without doing anything
B. It causes an infinite loop leading to a stack overflow error
C. It executes only once and stops
D. It throws a syntax error
Solution
Step 1: Understand the role of base case in recursion
The base case stops the recursion by providing a condition to end calls.
Step 2: Analyze what happens without a base case
Without a base case, the function keeps calling itself endlessly, filling the call stack.
Final Answer:
It causes an infinite loop leading to a stack overflow error -> Option B
Quick Check:
No base case = infinite recursion = stack overflow [OK]
Hint: Always check for a base case to stop recursion [OK]
Common Mistakes:
Thinking recursion stops automatically
Confusing base case with recursive case
Assuming syntax error occurs without base case
2. Which of the following is the correct syntax to define a recursive function countDown in JavaScript?
easy
A. function countDown(n) { if(n <= 0) return; countDown(n - 1); }
B. function countDown(n) { while(n > 0) countDown(n - 1); }
C. function countDown(n) { for(let i=0; i
D. function countDown(n) { if(n < 0) countDown(n + 1); }
Solution
Step 1: Identify correct base case and recursive call
function countDown(n) { if(n <= 0) return; countDown(n - 1); } has a base case (if n <= 0 return) and calls itself with n-1.
Step 2: Check other options for syntax or logic errors
function countDown(n) { while(n > 0) countDown(n - 1); } uses while loop causing infinite recursion; C uses for loop incorrectly; D lacks proper base case.
Final Answer:
function countDown(n) { if(n <= 0) return; countDown(n - 1); } -> Option A
Quick Check:
Base case + recursive call = correct syntax [OK]
Hint: Look for base case and self-call in function [OK]
Common Mistakes:
Using loops instead of recursion
Missing base case
Wrong recursive call argument
3. What is the output of the following code?
function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(3));