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 returning a direct answer without calling itself again.
Click to reveal answer
intermediate
How does the call stack help in recursion?
The call stack keeps track of each recursive call, saving where to return after each call finishes, so the function can complete step by step.
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 runs out of space.
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 multiplies back up the chain to get the final result.
Click to reveal answer
What does a recursive function always need to avoid infinite calls?
AAn array
BA base case
CA global variable
DA loop
✗ Incorrect
A base case stops the recursion by providing a simple answer without further calls.
In recursion, what does the call stack store?
AThe final result only
BOnly the first function call
CEach function call's return address and variables
DThe program's input
✗ Incorrect
The call stack stores information about each active function call to return correctly after finishing.
What will happen if a recursive function never reaches its base case?
ANo output
BProgram runs faster
CReturns zero
DStack overflow error
✗ Incorrect
Without a base case, recursion never stops, causing the call stack to overflow.
Which of these is a correct base case for factorial(n)?
Aif (n == 1) return 1;
Bif (n == 0) return 0;
Cif (n > 1) return n * factorial(n - 1);
Dif (n < 0) return -1;
✗ Incorrect
Factorial of 1 is 1, so returning 1 stops recursion correctly.
What is the output of factorial(3) using recursion?