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 iteration in programming?
Iteration means repeating a set of instructions using loops like for or while until a condition is met.
Click to reveal answer
intermediate
When is recursion better than iteration?
Recursion is better when the problem is naturally divided into similar smaller problems, like tree traversals or factorial calculation, making the code simpler and easier to understand.
Click to reveal answer
intermediate
When is iteration better than recursion?
Iteration is better when performance and memory use are important because it uses less memory and avoids the overhead of function calls, such as in simple loops or large data processing.
Click to reveal answer
intermediate
What is a risk of using recursion in C?
Using recursion in C can cause stack overflow if the recursion is too deep because each function call uses memory on the call stack.
Click to reveal answer
Which method uses function calls to solve smaller parts of a problem?
ABoth
BIteration
CNeither
DRecursion
✗ Incorrect
Recursion solves problems by calling the same function with smaller inputs.
Which method generally uses less memory in C?
ARecursion
BBoth use the same
CIteration
DDepends on compiler
✗ Incorrect
Iteration uses loops and does not add function calls to the stack, so it uses less memory.
Which is a natural fit for problems like tree traversal?
AIteration
BRecursion
CNeither
DBoth equally
✗ Incorrect
Recursion matches the structure of trees, making traversal easier to write.
What can happen if recursion is too deep in C?
AStack overflow
BFaster execution
CMemory leak
DNo effect
✗ Incorrect
Too many recursive calls can exhaust the call stack, causing a stack overflow.
Which method is usually easier to debug for simple repeated tasks?
AIteration
BRecursion
CBoth are equally easy
DNeither
✗ Incorrect
Iteration uses loops which are straightforward and easier to follow for simple tasks.
Explain when you would choose recursion over iteration and why.
Think about problems that look like smaller copies of themselves.
You got /3 concepts.
Describe the risks and limitations of using recursion in C.
Consider what happens when too many functions call themselves.
You got /3 concepts.
Practice
(1/5)
1. Which situation is best suited for using recursion instead of iteration?
easy
A. When you want to avoid function calls
B. When the problem can be divided into similar smaller problems
C. When memory usage must be minimal
D. When the problem requires simple repeated loops
Solution
Step 1: Understand recursion use case
Recursion works well when a problem breaks down into smaller similar problems, like factorial or tree traversal.
Step 2: Compare with iteration use cases
Iteration is better for simple repeated loops without breaking problems into smaller parts.
Final Answer:
When the problem can be divided into similar smaller problems -> Option B
Quick Check:
Recursion = divide and conquer [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
Thinking recursion is always better for memory
Confusing iteration as better for divide-and-conquer
Ignoring function call overhead in recursion
2. Which of the following is the correct syntax for a recursive function in C?
easy
A. void func() { if(condition) func(); }
B. void func() { func(); }
C. void func() { while(true) { func(); } }
D. void func() { for(int i=0; i<10; i++) func(); }
Solution
Step 1: Identify recursive call with base condition
Recursive functions must have a condition to stop calling themselves to avoid infinite recursion.
Step 2: Check options for base condition
void func() { if(condition) func(); } calls itself only if a condition is true, which is correct recursive syntax.