GATE Computer ScienceVery hardGATE CS (PYQ pattern)

int f(int n) { static int c = 0; c += n; if (n > 0) f(n - 1); return c; } int main() { int x = f(4); int y = f(2); printf("%d", x + y); } What is printed?

  1. A. 13
  2. B. 20
  3. C. 23
  4. D. 26

Correct answer: C. 23

c is static and persists. f(4) adds 4+3+2+1+0 = 10, and returns the final c = 10. f(2) adds 2+1+0 = 3 on top of 10, returning 13. So x + y = 10 + 13 = 23.

More Programming and Data Structures (GATE) questions

Exams that ask this topic