Question 111Asked in BPSC TRE 1.0 (2023) Computer Science Class 11-12

What will be the output of the following C++ code? long factorial (long a)

What will be the output of the following C++ code? #include<iostream> using namespace std; long factorial (long a) { if (a>1) return (a * factorial (a+1)); else return(1); } int main( ) { long num = 3; cout <<num<<"!="<<factorial (num); return 0; }
  1. A6
  2. B24
  3. CSegmentation fault
  4. DMore than one of the above
  5. ENone of the above
Show answerHide answer

Correct answer: (C) Segmentation fault

Explanation

factorial(a + 1) makes the argument grow (3, 4, 5, ...), so a > 1 is never false. The recursion never ends, the stack overflows, and the program crashes with a segmentation fault.

From BPSC TRE 1.0 (2023) Computer Science Class 11-12 — Previous Year Paper

This is question 111 of BPSC TRE 1.0 (2023) Computer Science Class 11-12 — Previous Year Paper. See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.