What will the following C++ code print?

#include <iostream>
using namespace std;
int factorial(int n)
{
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}
int main()
{
    cout << factorial(0);
    return 0;
}
                        

Options

  • B. 1
  • C. Undefined reference
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. 1

Detailed Explanation

Correct Answer: Option B

Explanation:
The input is already 0, so the base case executes immediately and returns 1. By definition, 0! = 1.