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 all the active function calls. Each recursive call adds a new layer to the stack, and when a base case is reached, the stack unwinds as functions return their results.
Click to reveal answer
beginner
Rust recursive function example: What does this function do?
fn factorial(n: u32) -> u32 {
if n == 0 { 1 } else { n * factorial(n - 1) }
}
This function calculates the factorial of a number n. It multiplies n by the factorial of n-1 until it reaches 0, where it returns 1 as the base case.
Click to reveal answer
intermediate
What happens if a recursive function has no base case?
Without a base case, the recursive calls never stop, causing the program to run forever or crash with a stack overflow error.
Click to reveal answer
What is the purpose of the base case in recursion?
ATo call the function again
BTo stop the recursion and return a simple answer
CTo increase the call stack size
DTo make the function run faster
✗ Incorrect
The base case stops recursion by providing a direct answer without further calls.
In Rust, what does this recursive function compute?
fn sum(n: u32) -> u32 {
if n == 0 { 0 } else { n + sum(n - 1) }
}
AFibonacci number at position n
BFactorial of n
CSum of numbers from 1 to n
DProduct of numbers from 1 to n
✗ Incorrect
The function adds n to the sum of numbers below it until 0, calculating the sum from 1 to n.
What does the call stack do during recursion?
ARuns all calls in parallel
BDeletes previous calls immediately
CPrevents recursion from happening
DStores each recursive call until base case is reached
✗ Incorrect
The call stack stores each active call so the program can return results in order.
What error occurs if recursion never reaches a base case?
AStack overflow
BSyntax error
CType error
DMemory leak
✗ Incorrect
Without a base case, calls pile up until the call stack runs out of space, causing a stack overflow.
Which of these is a correct base case for a factorial function?
Aif n == 0 { return 1 }
Bif n == 1 { return 0 }
Cif n > 0 { return n }
Dif n < 0 { return n }
✗ Incorrect
Factorial of 0 is 1, so this base case stops recursion correctly.
Explain how recursion works using the example of calculating factorial of 3.
Think about how factorial(3) calls factorial(2), then factorial(1), then factorial(0).
You got /4 concepts.
Describe the role of the call stack in managing recursive function calls.
Imagine stacking plates; each call adds a plate, and returning removes one.
You got /4 concepts.
Practice
(1/5)
1. What happens when a recursive function calls itself without a base case?
easy
A. The function converts into a loop automatically.
B. The function returns immediately without doing anything.
C. The function runs only once and stops.
D. It causes infinite recursion leading to a stack overflow error.
Solution
Step 1: Understand recursion base case role
The base case stops recursion by providing a condition to end calls.
Step 2: Analyze missing base case effect
Without a base case, the function keeps calling itself endlessly, filling the call stack.
Final Answer:
It causes infinite recursion leading to a stack overflow error. -> Option D
Quick Check:
Missing base case = infinite recursion [OK]
Hint: Always check for a base case to stop recursion [OK]
Common Mistakes:
Thinking recursion stops automatically
Assuming function returns without base case
Confusing recursion with loops
2. Which of the following is the correct syntax to define a recursive function in Rust that calculates factorial of a number n?
easy
A. fn factorial(n: u32) { if n == 0 { 1 } else { n * factorial(n - 1) } }
B. fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } }
C. fn factorial(n: u32) -> u32 { while n > 0 { n * factorial(n - 1) } }
D. fn factorial(n: u32) -> u32 { return n * factorial(n + 1); }
Solution
Step 1: Check function return type and base case
fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } correctly returns u32 and has base case for n == 0 returning 1.
Step 2: Verify recursive call correctness
fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } calls factorial(n - 1) correctly; others miss return type or have wrong logic.
Final Answer:
fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } -> Option B
Quick Check:
Correct syntax = fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } [OK]
Hint: Look for base case and correct return type in Rust function [OK]
Common Mistakes:
Omitting return type in function signature
Using while loop inside recursive function
Calling factorial with n + 1 causing infinite recursion