GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Consider this pseudo-code (arrays are 1-based): i = 1; a = [5, 6, 7]; procedure P(x, y) { x = x + 1; y = y * 2; } P(i, a[i]); print(i, a[1], a[2]); With call by name (the actual argument expression is re-evaluated at every use), the printed values are:

  1. A. 2 12 7
  2. B. 1 6 7
  3. C. 2 6 14
  4. D. 2 6 7

Correct answer: C. 2 6 14

Under call by name, x = x + 1 changes i to 2. The statement y = y * 2 then re-evaluates a[i], which is now a[2], so a[2] becomes 14 and a[1] stays 6. Call by reference would bind y to a[1] at call time and print 2 12 7.

More Compiler Design questions

Exams that ask this topic