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?
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2014 Set 2 · Q42
Time
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?Select one answerSingle correct
Solution
Official answer: D) The function will exhaust the runtime stack or run into an infinite loop when j = 50.
RecursionLet us trace the execution of the function f(int j):
Inside f, the variable i is declared as static int i = 50;. This means i is initialized once to and retains its value across all calls.
When f(50) is called (i.e., ):
The condition if (i == j) evaluates to if (50 == 50), which is true.
It prints "something".
Then it executes k = f(i);, which is another recursive call f(50).
In this recursive call, since i is still and is , the condition i == j is satisfied again, causing another recursive call f(50).
There is no base condition to terminate this recursion when . This leads to infinite recursion, which will eventually exhaust the runtime call stack (stack overflow).
Therefore, when , the function will exhaust the runtime stack or run into an infinite loop, making (D) the correct statement.
Try the question first, then check your work.
Examiner trap
Analyze what happens when j == 50 during the recursive call k = f(i). Does the value of i change between calls?
This Recursion previous year question is Q42 of the GATE CSE 2014 Set 2 paper. Find more GATE CSE PYQs on Recursion in Programming and Data Structures, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Recursion PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Recursion has 19 GATE CSE PYQs across 15 papers from 2011 to 2026. 10 of them are 2-mark MCQs like this one, and GATE CSE 2014 Set 2 has 2.
How GATE tests RecursionIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.