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 the base case in a recursive factorial function?
The base case is when the input number is 0 or 1, and the factorial is 1. This stops the recursion from continuing forever.
Click to reveal answer
intermediate
Explain how factorial of 4 is calculated using recursion.
Factorial(4) calls Factorial(3), which calls Factorial(2), then Factorial(1). Factorial(1) returns 1. Then results multiply back: 1 * 2 = 2, 2 * 3 = 6, 6 * 4 = 24.
Click to reveal answer
beginner
What happens if a recursive function does not have a base case?
Without a base case, the function keeps calling itself forever, causing a stack overflow error and crashing the program.
Click to reveal answer
beginner
Write the recursive formula for factorial n!
Factorial(n) = n * Factorial(n - 1), with Factorial(0) = 1 as the base case.
Click to reveal answer
What is the base case for factorial recursion?
AWhen n is negative
BWhen n is 2
CWhen n is 0 or 1
DWhen n is greater than 10
✗ Incorrect
The base case stops recursion and factorial(0) and factorial(1) both equal 1.
What does factorial(3) return in recursion?
A6
B3
C9
D1
✗ Incorrect
Factorial(3) = 3 * factorial(2) = 3 * 2 = 6.
What is the risk of missing a base case in recursion?
AFaster execution
BInfinite recursion and crash
CNo output
DCorrect result
✗ Incorrect
Without a base case, recursion never stops, causing a stack overflow error.
Which of these is the correct recursive call in factorial function?
Afactorial(n - 1)
Bfactorial(n + 1)
Cfactorial(n * 2)
Dfactorial(n / 2)
✗ Incorrect
Factorial uses factorial(n - 1) to reduce the problem size step by step.
What is factorial(0)?
A-1
B0
CUndefined
D1
✗ Incorrect
By definition, factorial(0) is 1.
Explain how recursion works in calculating factorial with an example.
Think about how the function breaks the problem into smaller parts and then combines answers.
You got /4 concepts.
Describe why a base case is important in recursive functions.
Without it, the function would never stop calling itself.
You got /4 concepts.
Practice
(1/5)
1. What does the factorial of a number represent in mathematics?
easy
A. The number multiplied by 2
B. The product of all positive integers up to that number
C. The sum of all positive integers up to that number
D. The number divided by 2
Solution
Step 1: Understand factorial definition
Factorial of a number n (written as n!) is the product of all positive integers from 1 to n.
Step 2: Compare options with definition
Only The product of all positive integers up to that number correctly describes factorial as a product, not sum or division.
Final Answer:
The product of all positive integers up to that number -> Option B
Quick Check:
Factorial = product of integers [OK]
Hint: Factorial multiplies numbers down to 1 [OK]
Common Mistakes:
Confusing factorial with sum
Thinking factorial divides the number
Assuming factorial doubles the number
2. Which of the following is the correct base case for a recursive factorial function in Java?
easy
A. if (n == 1) return n + 1;
B. if (n == 1) return 0;
C. if (n == 0) return 0;
D. if (n == 0) return 1;
Solution
Step 1: Identify factorial base case
Factorial of 0 is defined as 1, so base case returns 1 when n == 0.
Step 2: Check options for correct base case
Only if (n == 0) return 1; returns 1 when n == 0, others return incorrect values.
Final Answer:
if (n == 0) return 1; -> Option D
Quick Check:
Base case n=0 returns 1 [OK]
Hint: Base case for factorial is n=0 returns 1 [OK]
Common Mistakes:
Returning 0 for base case
Using n==1 with wrong return value
Missing base case causing infinite recursion
3. What is the output of the following Java code?
public class Main {
public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
System.out.println(factorial(4));
}
}