GateAI
Recursion

GATE CSE 2013 · Q42

MCQ2 marksMedium

Time

What is the return value of f(p, p), if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.int f (int &x, int c) { c = c - 1; if (c==0) return 1; x = x + 1; return f(x,c) * x; }

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: See verified solution

Recursion

Let us trace the recursive execution when p is initially 55 and f(p, p) is invoked:

  1. In f(int &x, int c), x is passed by reference to p (meaning any modification to x modifies p directly), while c is passed by value.

  2. Call 1: c=5−1=4c = 5 - 1 = 4; xx becomes 5+1=65 + 1 = 6 (p=6p = 6). Computes return f(x, 4) * x;.

  3. Call 2: c=4−1=3c = 4 - 1 = 3; xx becomes 6+1=76 + 1 = 7 (p=7p = 7). Computes return f(x, 3) * x;.

  4. Call 3: c=3−1=2c = 3 - 1 = 2; xx becomes 7+1=87 + 1 = 8 (p=8p = 8). Computes return f(x, 2) * x;.

  5. Call 4: c=2−1=1c = 2 - 1 = 1; xx becomes 8+1=98 + 1 = 9 (p=9p = 9). Computes return f(x, 1) * x;.

  6. Call 5: c=1−1=0c = 1 - 1 = 0; the base case condition if (c == 0) is met, returning 11.

In standard C/C++, the order of evaluation of operands in the multiplication operator f(x, c) * x is unspecified:

  • If x is evaluated after the recursive call returns, xx evaluates to the latest value of pp (99) in all stack frames, yielding:

    1×9×9×9×9=94=6561(Option B)1 \times 9 \times 9 \times 9 \times 9 = 9^4 = 6561 \quad (\text{Option B})
  • If x is evaluated before the recursive call, the multiplication uses the value of xx at each frame (9,8,7,69, 8, 7, 6), yielding:

    1×9×8×7×6=3024(Option A)1 \times 9 \times 8 \times 7 \times 6 = 3024 \quad (\text{Option A})

Due to this ambiguity in compiler evaluation order, full marks were awarded to all candidates (Marks to All).

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 2013 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 it is the only one in GATE CSE 2013.

How GATE tests Recursion

More from GATE CSE 2013

Share this solution

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

GateAI solution cardGATE CSE 2013 · Q42 · MCQ

What is the return value of f(p, p) , if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value. int f (int &x, int c) { c = c - 1; if (c==0) return 1; x = x + 1; return f(x,c) * x; }

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.