What will be the output of the following C++ code?

2023

What will be the output of the following C++ code?

  1. A.

    6

  2. B.

    24

  3. C.

    Segmentation fault

  4. D.

    More than one of the above

  5. E.

    None of the above

Attempted by 150 students.

Show answer & explanation

Correct answer: C

The given C++ code has a logical error in the recursive function `factorial`. The function is defined as `factorial(a+1)` instead of `factorial(a-1)`. This means the value of `a` increases with each recursive call, so the base case `a <= 1` is never reached. As a result, the recursion continues indefinitely, leading to a stack overflow. In most systems, this causes a segmentation fault. Therefore, the program will not produce a numerical output but will crash with a segmentation fault.

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs