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
Why do we need recursion when we have loops?
Some problems, like exploring tree structures or solving puzzles, are easier and cleaner to solve with recursion because loops can't naturally handle nested or branching steps.
Click to reveal answer
intermediate
Give an example of a problem that loops cannot express cleanly but recursion can.
Walking through a family tree or calculating Fibonacci numbers is easier with recursion because each step depends on smaller similar steps, which loops find hard to represent clearly.
Click to reveal answer
beginner
What is a base case in recursion?
A base case is the simplest part of the problem that recursion can solve directly without calling itself again, stopping the recursive calls.
Click to reveal answer
intermediate
How does recursion help in expressing problems with nested or branching structures?
Recursion naturally breaks down problems into smaller similar problems, making it easy to handle nested or branching structures like trees or graphs, which loops struggle to manage cleanly.
Click to reveal answer
What stops a recursive function from calling itself forever?
AA base case that stops further calls
BA loop inside the function
CA timer that interrupts the function
DA global variable
✗ Incorrect
The base case is the condition that stops recursion by solving the simplest problem directly.
Which of these problems is best solved using recursion rather than loops?
ASumming numbers from 1 to 10
BPrinting numbers from 1 to 10
CFinding the height of a family tree
DCalculating the average of a list
✗ Incorrect
Family trees have branching structures that recursion handles naturally, unlike simple loops.
What is a key limitation of loops compared to recursion?
ALoops cannot handle nested or branching problems cleanly
BLoops use more memory
CLoops are slower than recursion
DLoops cannot repeat actions
✗ Incorrect
Loops struggle to express problems with nested or branching steps clearly, which recursion can handle well.
In recursion, what happens after the base case is reached?
AThe function enters an infinite loop
BThe function stops and returns results back through previous calls
CThe function restarts from the beginning
DThe function calls another function
✗ Incorrect
After reaching the base case, recursive calls return their results back up the chain.
Which statement about recursion is true?
ARecursion never needs a base case
BRecursion always uses less memory than loops
CRecursion cannot be used in C language
DRecursion is useful for problems that can be divided into similar smaller problems
✗ Incorrect
Recursion works well when a problem can be broken into smaller similar problems solved the same way.
Explain why recursion is necessary and what kinds of problems loops cannot express cleanly.
Think about problems with branching steps or nested structures.
You got /4 concepts.
Describe the role of the base case in recursion and what would happen without it.
Consider what stops the function from calling itself endlessly.
You got /4 concepts.
Practice
(1/5)
1. Why do programmers use recursion instead of loops in some problems?
easy
A. Because loops cannot be written in C language
B. Because loops are slower than recursion in all cases
C. Because recursion can express problems with self-similar structure more clearly
D. Because recursion uses less memory than loops
Solution
Step 1: Understand recursion's purpose
Recursion is used to solve problems that can be broken down into smaller similar problems, like tree traversals or factorial calculation.
Step 2: Compare recursion and loops
Loops are good for repeated actions but cannot easily express problems with nested or self-similar structure without complex code.
Final Answer:
Because recursion can express problems with self-similar structure more clearly -> Option C
Quick Check:
Recursion clarity = Because recursion can express problems with self-similar structure more clearly [OK]
Hint: Recursion fits self-similar problems better than loops [OK]
Common Mistakes:
Thinking loops are always faster than recursion
Believing recursion always uses less memory
Assuming loops cannot be written in C
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) { for (int i = 1; i <= n; i++) return i * factorial(i - 1); }
B. int factorial(int n) { while (n > 0) return n * factorial(n - 1); }
C. int factorial(int n) { if (n == 1) 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
int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } correctly returns 1 when n is 0, which is the factorial base case.
Step 2: Check recursive call and return
int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } multiplies n by factorial(n-1), correctly defining factorial recursively.
Final Answer:
int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } -> Option D
Quick Check:
Correct factorial recursion syntax = int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } [OK]
Hint: Look for correct base case and recursive call [OK]
Common Mistakes:
Missing base case or wrong base case
Using loops inside recursive function incorrectly
Returning wrong values in base case
3. What is the output of the following C code?
int sum(int n) {
if (n == 0) return 0;
else return n + sum(n - 1);
}
int main() {
printf("%d", sum(3));
return 0;
}