Question

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;
}
Choose one option
Official answer and solution
Official answer
Marks to all
From
Official GATE 2013 answer key
Why the marks went to every candidate

Official GATE answer key awarded Marks to All for this question.

Solution

Trace the recursive execution of f(p, p) and examine the order of evaluation of multiplication operands with pass-by-reference.

The function definition is:

int f (int &x, int c) {
    c = c - 1;
    if (c == 0) return 1;
    x = x + 1;
    return f(x, c) * x;
}

We start with variable p = 5 and call f(p, p).

  • Parameter x is passed by reference to p. Any modification to x directly updates p.
  • Parameter c is passed by value (starts with a copy of value 5).

Let us trace the successive recursive calls:

  1. Call 1: p = 5. x refers to p, c = 5.
    • c becomes 5−1=45 - 1 = 4.
    • x becomes 5+1=65 + 1 = 6 (so p is now 6).
    • Evaluates return f(x, 4) * x;.
  2. Call 2: x refers to p (currently 6), c = 4.
    • c becomes 4−1=34 - 1 = 3.
    • x becomes 6+1=76 + 1 = 7 (so p is now 7).
    • Evaluates return f(x, 3) * x;.
  3. Call 3: x refers to p (currently 7), c = 3.
    • c becomes 3−1=23 - 1 = 2.
    • x becomes 7+1=87 + 1 = 8 (so p is now 8).
    • Evaluates return f(x, 2) * x;.
  4. Call 4: x refers to p (currently 8), c = 2.
    • c becomes 2−1=12 - 1 = 1.
    • x becomes 8+1=98 + 1 = 9 (so p is now 9).
    • Evaluates return f(x, 1) * x;.
  5. Call 5: x refers to p (currently 9), c = 1.
    • c becomes 1−1=01 - 1 = 0.
    • Condition c == 0 is met, so it returns 1.

Now consider the expression return f(x, c) * x;:
In C and C++, the order of evaluation of the operands of the binary * operator is unspecified:

  • Case 1 (evaluate x after f(x, c) returns):
    When Call 5 returns 1 to Call 4, x (which refers to p) has value 9. Call 4 returns 1×9=91 \times 9 = 9.
    At Call 3, x is 9. It returns 9×9=819 \times 9 = 81.
    At Call 2, x is 9. It returns 81×9=72981 \times 9 = 729.
    At Call 1, x is 9. It returns 729×9=6561729 \times 9 = 6561 (Option B).

  • Case 2 (evaluate x before calling f(x, c)):
    Call 4 evaluates 1×9=91 \times 9 = 9.
    Call 3 evaluates 9×8=729 \times 8 = 72.
    Call 2 evaluates 72×7=50472 \times 7 = 504.
    Call 1 evaluates 504×6=3024504 \times 6 = 3024 (Option A).

Because the evaluation order of operands in multiplication is unspecified in the language standard, both 3024 and 6561 are defensible results depending on compiler implementation. Consequently, GATE officially awarded Marks to All.

Final answer

Marks to All

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2013 Computer Science and Information Technology (CS)
Question
42 of 65, printed as CS Q.42
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.