GATE Computer ScienceVery hardGATE CS (PYQ pattern)

int i = 1; int A[1..4] = {4, 7, 2, 9}; void swap(x, y) { int t = x; x = y; y = t; } The call swap(i, A[i]) is made. The parameters are passed by name (each use of x or y re-evaluates the argument expression in the caller's environment). What are the values after the call?

  1. A. i = 4; A[1..4] = 1, 7, 2, 9
  2. B. i = 4; A[1..4] = 4, 7, 2, 1
  3. C. i = 1; A[1..4] = 4, 7, 2, 9
  4. D. i = 4; A[1..4] = 4, 7, 2, 9

Correct answer: B. i = 4; A[1..4] = 4, 7, 2, 1

By name: t = i = 1; then i = A[i] = A[1] = 4; then A[i] = t becomes A[4] = 1 (i is now 4). With reference the second store would go to A[1], the element chosen at call time.

More Compiler Design questions

Exams that ask this topic