Skip to content
Official PYQ · 2014

GATE CS 2014 Set 2 Q42 — Programming in C

Explain with AI

Consider the C function given below.

int f(int j)
{ 
    static int i = 50; 
    int k; 
    if (i == j) 
    { 
        printf("something"); 
        k = f(i); 
        return 0; 
    } 
    else return 0; 
} 

Which one of the following is TRUE?

Choose one option
View worked solution

Answer: D

Let us trace the execution of the function f(j)f(j) when j=50j = 50:

  1. The static variable i is initialized to 5050 (this happens only once at the start of the program).
  2. When we call f(50), the parameter j is 5050.
  3. The condition if (i == j) evaluates to 50 == 50, which is true.
  4. The function prints "something" and then recursively calls k = f(i), which is f(50).
  5. This recursive call f(50) repeats the exact same sequence of steps, leading to an infinite recursion.

Because there is no base case to terminate this recursion when j=50j = 50, the program will continuously push new activation records onto the call stack, eventually resulting in a stack overflow (exhausting the runtime stack).

Thus, option D is the correct choice.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions