The GATE Grind

GATE 2014 CS (CS2) – Question 52

Programming and Data Structures · Recursion · 2 marks · Multiple choice

Consider the 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?

  1. Returns 0 for every j
  2. Prints “something” for every j
  3. Returns 0 for j = 50
  4. Exhausts the runtime stack or loops infinitely for j = 50

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (D) Exhausts the runtime stack or loops infinitely for j = 50

Explanation

Static i is initialised once to50 and is never changed. For j=50 the condition stays true in every recursive call f(i), repeatedly printing and recursing without reaching a return. Thus **D**, stack exhaustion or infinite recursion occurs. Other jvalues return0 immediately.