GateAI
Recursion

GATE CSE 2014 Set 2 · Q42

MCQ2 marksMedium

Time

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?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: D) The function will exhaust the runtime stack or run into an infinite loop when j = 50.

Recursion

Let us trace the execution of the function f(int j):

  1. Inside f, the variable i is declared as static int i = 50;. This means i is initialized once to 5050 and retains its value across all calls.

  2. When f(50) is called (i.e., j=50j = 50):

    • 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).

  3. In this recursive call, since i is still 5050 and jj is 5050, the condition i == j is satisfied again, causing another recursive call f(50).

  4. There is no base condition to terminate this recursion when j=50j = 50. This leads to infinite recursion, which will eventually exhaust the runtime call stack (stack overflow).

Therefore, when j=50j = 50, 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.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

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

Where do you stand for GATE?

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.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Recursion in GATE CSE

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 Recursion

More from GATE CSE 2014 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2014 Set 2 · Q42 · MCQ

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

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.