GATE / CS ARCHIVE

GATE CSE 2014 SET 2 / QUESTION 42

GATE CSE 2014 Set 2 Q42

Programming in C · Recursion · Full GATE CSE 2014 Set 2 paper

GATE 2014 Set 2, Q42 2 marks · MCQ
Report issue

Recursion

Question 42, GATE 2014: For C function int f(int j){static int i=50; int k; if(i==j){printf("something"); k=f(i); return 0;} else return 0;}, which is true? A returns 0 for every j; B prints something for every j; C returns 0 for j=50; D exhausts the runtime stack or loops indefinitely when j=50.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

1. Set up the reasoning. The static variable is initialized once and never modified. Split the trace into j≠50, which returns immediately, and j=50, which repeats exactly the same call state.

2. Derive the result. The static i remains 50 across calls. If j≠50, the function immediately returns 0. If j=50, it prints and calls f(50) again without changing the condition; no call reaches its following return. With ordinary recursive frames the stack is exhausted; an optimization eliminating redundant frames could instead run indefinitely. Thus D, assuming normal successful output operations.

3. Check the interpretation. The return statement after the recursive call is unreachable unless that call returns, and the recursion has no changing measure or base case for 50. Optimizations may affect stack growth but do not create the missing termination condition.

Original paper, page 41 · Answer key