GATE Computer ScienceHardGATE CS (PYQ pattern)

void p(int x, int y) { x = x + 1; y = y + x; } is called as p(a, a) with a = 2. What is a after the call when the parameters are passed by value, by reference, and by value-result (copy-out in the order x then y), respectively?

  1. A. 2, 6 and 3
  2. B. 2, 6 and 5
  3. C. 2, 5 and 6
  4. D. 3, 6 and 5

Correct answer: B. 2, 6 and 5

By value: a is untouched, 2. By reference: x and y are both a: x = x + 1 makes a = 3, then y = y + x makes a = 3 + 3 = 6. By value-result: local x = y = 2; x becomes 3 and y = 2 + 3 = 5; copy-out writes a = 3 and then a = 5, so 5 (copying y first would give 3).

More Compiler Design questions

Exams that ask this topic