Open In App

GATE | GATE-CS-2004 | Question 31

Consider the following C function: 




int f(int n)
{
   static int i = 1;
   if (n >= 5)
      return n;
   n = n+i;
   i++;
   return f(n);
}

The value returned by f(1) is



(A)

5



(B)

6

(C)

7

(D)

8


Answer: (C)
Explanation:

Let’s understand the problem with given Dry Run:

Initially, the static variable i is initialized to 1.

f(1) is called:

f(2) is called:

f(4) is called:

f(7) is called:

Now, the recursive calls start to end:

So, the value returned by f(1) is 7.

Hence (C) is the correct answer.

Quiz of this Question
Please comment below if you find anything wrong in the above post

Article Tags :