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 already have loops?
Recursion helps solve problems that have a natural nested or branching structure, like trees or puzzles, which loops find hard to express clearly.
Click to reveal answer
intermediate
Give an example of a problem that loops cannot express cleanly but recursion can.
Calculating Fibonacci numbers or traversing a family tree are easier to express with recursion because they involve repeating similar steps on smaller parts.
Click to reveal answer
beginner
What is a base case in recursion?
A base case is the simplest situation where the problem can be solved directly without more recursive calls, stopping the recursion.
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, matching the nested or branching shape, making the code simpler and clearer.
Click to reveal answer
What does recursion do in programming?
AA variable stores multiple values
BA loop repeats a block of code
CA function calls itself to solve smaller problems
DA function runs only once
✗ Incorrect
Recursion means a function calls itself to solve smaller parts of a problem.
Which problem is easier to solve with recursion than with loops?
ATraversing a family tree
BAdding numbers from 1 to 10
CPrinting numbers 1 to 5
DCalculating the sum of an array
✗ Incorrect
Traversing a family tree has a branching structure that recursion handles naturally.
What stops a recursive function from calling itself forever?
AA base case that ends recursion
BA loop inside the function
CA variable increment
DA print statement
✗ Incorrect
The base case is the simple condition where recursion stops.
Why can loops be less clear than recursion for some problems?
ALoops are slower than recursion
BLoops do not work with numbers
CLoops cannot repeat code
DLoops struggle with nested or branching structures
✗ Incorrect
Loops are less natural for nested or branching problems, which recursion expresses clearly.
Which of these is NOT a reason to use recursion?
ATo solve problems with nested structures
BTo avoid using any loops
CTo break problems into smaller similar problems
DTo simplify code for branching problems
✗ Incorrect
Recursion is not used just to avoid loops; it is used when it fits the problem structure better.
Explain why recursion is useful for problems with nested or branching structures and why loops may not express these problems cleanly.
Think about how family trees or puzzles have parts inside parts.
You got /4 concepts.
Describe what a base case is in recursion and why it is important.
Without it, recursion would never stop.
You got /4 concepts.
Practice
(1/5)
1. Why do we use recursion instead of loops in some problems?
easy
A. Because loops are slower than recursion in all cases
B. Because loops cannot repeat tasks
C. Because recursion uses less memory than loops always
D. Because recursion can express problems with self-similar subproblems more naturally
Solution
Step 1: Understand problem structure
Some problems break down into smaller versions of themselves, like tree traversals or factorial calculation.
Step 2: Compare recursion and loops
Loops repeat tasks but do not naturally express self-similar subproblems, while recursion calls itself to handle smaller parts.
Final Answer:
Because recursion can express problems with self-similar subproblems more naturally -> Option D
Quick Check:
Recursion fits self-similar problems = B [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 repeat tasks
2. Which of the following is the correct syntax to define a recursive method in Java?
easy
A. void recurse() { recurse(); }
B. void recurse() { while(true) recurse(); }
C. void recurse() { if(condition) recurse(); }
D. void recurse() { for(int i=0; i<10; i++) recurse(); }
Solution
Step 1: Identify base condition in recursion
Recursive methods must have a condition to stop calling themselves to avoid infinite recursion.
Step 2: Check options for base condition
void recurse() { if(condition) recurse(); } uses an if(condition) to control recursion, which is correct. Others either lack a base case or use loops incorrectly.
Final Answer:
void recurse() { if(condition) recurse(); } -> Option C
Quick Check:
Recursion needs base condition = A [OK]
Hint: Look for base condition controlling recursion call [OK]
Common Mistakes:
Missing base condition causing infinite recursion
Using loops inside recursion incorrectly
Confusing recursion call with loop iteration
3. What is the output of the following Java code?
public class Test {
static int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
public static void main(String[] args) {
System.out.println(factorial(4));
}
}