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 where it stops calling itself and returns a result directly.
Click to reveal answer
intermediate
How does the call stack help in recursion?
The call stack keeps track of all the function calls waiting to finish. Each recursive call adds a new layer, and when a base case is reached, the stack unwinds to return results.
Click to reveal answer
beginner
What happens if a recursive function has no base case?
Without a base case, the function keeps calling itself forever, causing the program to crash with a stack overflow error.
Click to reveal answer
intermediate
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 multiplies all the returned values step by step to get the final answer.
Click to reveal answer
What does a recursive function always need to stop calling itself?
AA loop
BA base case
CA global variable
DAn error handler
✗ Incorrect
A base case tells the recursive function when to stop calling itself.
What does the call stack do during recursion?
ARuns all calls in parallel
BDeletes previous calls immediately
CStores each recursive call until base case is reached
DPrevents recursion from happening
✗ Incorrect
The call stack stores each call so the program can return results in the right order.
What error occurs if recursion never reaches a base case?
AStack overflow
BSyntax error
CType error
DMemory leak
✗ Incorrect
Without a base case, the call stack grows too large causing a stack overflow.
In the factorial example, what is the base case?
AWhen n equals 0
BWhen n is greater than 10
CWhen n is negative
DWhen n equals 1
✗ Incorrect
The factorial function stops recursion when n equals 1.
Which of these is NOT true about recursion?
AIt never needs a base case
BIt always uses a call stack
CIt can be replaced by loops
DIt solves problems by breaking them down
✗ Incorrect
Recursion always needs a base case to stop calling itself.
Describe how recursion works using the call stack and base case.
Think about how each call waits for the next one to finish.
You got /4 concepts.
Explain why a base case is important in a recursive function.
What happens if the function never stops calling itself?
You got /4 concepts.
Practice
(1/5)
1. What happens when a recursive function calls itself without a stopping condition?
easy
A. It converts into a normal loop automatically
B. It immediately returns without doing anything
C. It runs only once and stops
D. It causes an infinite loop and eventually a stack overflow error
Solution
Step 1: Understand recursion without base case
Without a stopping condition, the function keeps calling itself endlessly.
Step 2: Effect on call stack
Each call adds a new frame to the call stack, which grows until memory runs out causing stack overflow.
Final Answer:
It causes an infinite loop and eventually a stack overflow error -> Option D
Quick Check:
Missing base case = infinite recursion [OK]
Hint: Always check for a base case to stop recursion [OK]
Common Mistakes:
Thinking recursion stops automatically
Confusing recursion with loops
Ignoring stack overflow possibility
2. Which of the following is the correct syntax to define a recursive function in Go that calculates factorial of n?
easy
A. func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) }
B. func factorial(n int) { if n == 0 { return 1 } return n * factorial(n-1) }
C. func factorial(n int) int { if n == 0 { return 0 } return n * factorial(n-1) }
D. func factorial(n int) int { if n == 1 { return 0 } return n * factorial(n-1) }
Solution
Step 1: Check function signature and return type
Factorial returns an int, so function must return int type.
Step 2: Verify base case and recursive call
Base case is n == 0 returning 1; recursive call multiplies n by factorial(n-1).
Final Answer:
func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } -> Option A
Quick Check:
Correct base case and return type = func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } [OK]