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.
Click to reveal answer
intermediate
When is iteration better than recursion?
Iteration is better when you want to save memory and avoid the overhead of function calls, especially for simple loops or large data sets.
Click to reveal answer
intermediate
What is a risk of using recursion?
Recursion can cause a stack overflow if it goes too deep because each call uses memory on the call stack.
Click to reveal answer
Which method uses function calls to solve smaller parts of a problem?
ANeither
BIteration
CBoth
DRecursion
✗ Incorrect
Recursion solves problems by calling itself with smaller inputs.
Which is generally more memory efficient for large loops?
ARecursion
BIteration
CBoth use same memory
DDepends on language
✗ Incorrect
Iteration uses loops and does not add extra call stack memory.
Which scenario is best suited for recursion?
ACalculating factorial
BSimple counting loop
CReading a file line by line
DSorting an array with loops
✗ Incorrect
Factorial naturally breaks down into smaller factorials, ideal for recursion.
What problem can deep recursion cause?
AStack overflow
BInfinite loop
CMemory leak
DSyntax error
✗ Incorrect
Each recursive call uses stack memory; too many calls cause overflow.
Which is easier to debug for simple repeated tasks?
ARecursion
BBoth equal
CIteration
DDepends on compiler
✗ Incorrect
Iteration uses straightforward loops, making debugging simpler for simple tasks.
Explain when you would choose recursion over iteration and why.
Think about problems that look like smaller versions of themselves.
You got /4 concepts.
Describe the main risks or downsides of using recursion compared to iteration.
Consider what happens inside the computer when functions call themselves many times.
You got /4 concepts.
Practice
(1/5)
1. Which scenario is best suited for using recursion instead of iteration?
easy
A. When memory usage must be minimal
B. When you want to avoid function call overhead
C. When the problem can be divided into similar smaller problems
D. When the problem requires simple repeated loops
Solution
Step 1: Understand recursion use case
Recursion works well when a problem can be broken down into smaller similar problems, like factorial or tree traversal.
Step 2: Compare with iteration needs
Iteration is better for simple repeated loops or when avoiding function call overhead is important.
Final Answer:
When the problem can be divided into similar smaller problems -> Option C
Quick Check:
Recursion = divide and conquer [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
Thinking iteration is always better for all problems
Ignoring function call overhead in recursion
Confusing recursion with simple loops
2. Which of the following is the correct syntax for a recursive method in C#?
easy
A. void RecursiveMethod() { while(true) { RecursiveMethod(); } }
B. void RecursiveMethod() { if(condition) RecursiveMethod(); else return; }
C. void RecursiveMethod() { for(int i=0; i<10; i++) RecursiveMethod(); }
D. void RecursiveMethod() { RecursiveMethod(); }
Solution
Step 1: Identify base case in recursion
Recursive methods must have a base case to stop calling themselves, shown by the else return in void RecursiveMethod() { if(condition) RecursiveMethod(); else return; }.
Step 2: Check other options for missing base case
Options B, C, and D call themselves without stopping, causing infinite recursion.