Bird
Raised Fist0
DSA Javaprogramming~20 mins

Factorial Using Recursion in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Factorial Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Recursive Factorial Call
What is the output of the following Java code that calculates factorial recursively?
DSA Java
public class Factorial {
    public 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));
    }
}
A1
B10
C120
D24
Attempts:
2 left
💡 Hint
Remember factorial of 4 is 4 * 3 * 2 * 1.
❓ Predict Output
intermediate
2:00remaining
Output for Factorial of Zero
What will be printed when the factorial function is called with 0?
DSA Java
public class Factorial {
    public 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(0));
    }
}
AException
B1
C0
D-1
Attempts:
2 left
💡 Hint
Factorial of zero is defined as 1.
🔧 Debug
advanced
2:00remaining
Identify the Error in Recursive Factorial
What error will this code produce when run?
DSA Java
public class Factorial {
    public static int factorial(int n) {
        if (n == 1) {
            return 1;
        } else {
            return n * factorial(n - 1);
        }
    }

    public static void main(String[] args) {
        System.out.println(factorial(0));
    }
}
AStackOverflowError
B1
C0
DCompilation Error
Attempts:
2 left
💡 Hint
Check what happens when n is 0 in the recursive call.
🧠 Conceptual
advanced
2:00remaining
Recursion Depth for Factorial of 5
How many times is the factorial function called when calculating factorial(5)?
A4
B5
C6
D1
Attempts:
2 left
💡 Hint
Count the initial call plus each recursive call down to the base case.
🚀 Application
expert
3:00remaining
Output of Modified Factorial with Print Statements
What is the output of this Java program that prints each call and returns factorial?
DSA Java
public class Factorial {
    public static int factorial(int n) {
        System.out.println("Call factorial(" + n + ")");
        if (n == 0) {
            return 1;
        } else {
            int result = n * factorial(n - 1);
            System.out.println("Return " + result + " for factorial(" + n + ")");
            return result;
        }
    }

    public static void main(String[] args) {
        factorial(2);
    }
}
A
Call factorial(2)
Call factorial(1)
Call factorial(0)
Return 1 for factorial(1)
Return 2 for factorial(2)
B
Call factorial(2)
Call factorial(1)
Call factorial(0)
Return 1 for factorial(0)
Return 2 for factorial(1)
Return 2 for factorial(2)
C
Call factorial(2)
Call factorial(1)
Return 1 for factorial(1)
Call factorial(0)
Return 1 for factorial(0)
Return 2 for factorial(2)
D
Call factorial(2)
Call factorial(1)
Call factorial(0)
Return 0 for factorial(0)
Return 1 for factorial(1)
Return 2 for factorial(2)
Attempts:
2 left
💡 Hint
Trace the calls and returns carefully, noting the order of print statements.

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

  1. Step 1: Understand factorial definition

    Factorial of a number n (written as n!) is the product of all positive integers from 1 to n.
  2. 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.
  3. Final Answer:

    The product of all positive integers up to that number -> Option B
  4. 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

  1. Step 1: Identify factorial base case

    Factorial of 0 is defined as 1, so base case returns 1 when n == 0.
  2. Step 2: Check options for correct base case

    Only if (n == 0) return 1; returns 1 when n == 0, others return incorrect values.
  3. Final Answer:

    if (n == 0) return 1; -> Option D
  4. 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));
  }
}
medium
A. 24
B. 10
C. 16
D. 120

Solution

  1. Step 1: Trace recursive calls for factorial(4)

    factorial(4) = 4 * factorial(3), factorial(3) = 3 * factorial(2), factorial(2) = 2 * factorial(1), factorial(1) = 1 * factorial(0), factorial(0) = 1.
  2. Step 2: Calculate values bottom-up

    factorial(1) = 1*1=1, factorial(2)=2*1=2, factorial(3)=3*2=6, factorial(4)=4*6=24.
  3. Final Answer:

    24 -> Option A
  4. Quick Check:

    4! = 24 [OK]
Hint: Multiply down from n to 1 recursively [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Wrong base case causing wrong output
  • Confusing factorial(4) with sum
4. Find the error in this recursive factorial function:
public static int factorial(int n) {
  if (n == 1) return 1;
  return n * factorial(n);
}
medium
A. Return type should be void
B. Missing base case for n == 0
C. Recursive call uses same n causing infinite recursion
D. Multiplication operator is incorrect

Solution

  1. Step 1: Analyze recursive call parameter

    The function calls factorial(n) again without reducing n, causing infinite recursion.
  2. Step 2: Identify correct recursive call

    Recursive call should be factorial(n - 1) to reduce n and reach base case.
  3. Final Answer:

    Recursive call uses same n causing infinite recursion -> Option C
  4. Quick Check:

    Recursive call must reduce n [OK]
Hint: Recursive calls must reduce problem size [OK]
Common Mistakes:
  • Calling factorial(n) instead of factorial(n-1)
  • Missing base case for n=0
  • Wrong return type
5. How would you modify the recursive factorial function to handle negative inputs by returning -1 for any negative number?
public static int factorial(int n) {
  if (n == 0) return 1;
  return n * factorial(n - 1);
}
hard
A. Add if (n < 0) return -1; before base case
B. Change base case to if (n <= 0) return 1;
C. Add if (n == 1) return -1; after base case
D. No change needed; factorial handles negatives

Solution

  1. Step 1: Identify handling of negative inputs

    Factorial is undefined for negative numbers, so function should return -1 for n < 0.
  2. Step 2: Add condition before base case

    Insert if (n < 0) return -1; before checking base case to catch negatives early.
  3. Final Answer:

    Add if (n < 0) return -1; before base case -> Option A
  4. Quick Check:

    Negative input returns -1 [OK]
Hint: Check negatives first, return error value [OK]
Common Mistakes:
  • Changing base case incorrectly
  • Not handling negatives causing infinite recursion
  • Assuming factorial works for negatives
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard