Multiple choice

long factorial (long x) { ???? return x * factorial(x - 1); }

With what do you replace the ???? to make the function shown above return the correct answer?

  1. if (x == 0) return 0;

  2. return 1;

  3. if (x >= 2) return 2;

  4. if (x == 0) return 1;

  5. if (x <= 1) return 1;

Reveal answer Fill a bubble to check yourself
E Correct answer
Explanation

A factorial function needs a base case to terminate recursion. For factorial, 0! = 1 and 1! = 1. Option E handles this correctly: when x <= 1, return 1. Otherwise, it recursively computes x * factorial(x-1). Options A and D are incomplete (they don't handle x=1). Option B would cause infinite recursion for x > 1. Option C would return incorrect values.