GATE Computer ScienceVery hardGATE CSE · NIELIT

In a language with Algol-style call by name, int i = 1 and A = [10, 20, 30] (1-indexed) are global, and procedure P(p, q) { i = i + 1; p = p + q; } is called as P(A[i], i). What is the array after the call under call by name, call by reference, and call by value respectively?

  1. A. [10,22,30], [12,20,30], [10,20,30]
  2. B. [10,22,30], [10,22,30], [10,20,30]
  3. C. [12,20,30], [10,22,30], [10,20,30]
  4. D. [10,22,30], [12,20,30], [12,20,30]

Correct answer: A. [10,22,30], [12,20,30], [10,20,30]

By name, p is re-evaluated as A[i] with i = 2, so A[2] = 20 + 2 = 22. By reference, p is bound to A[1] and q to i, so A[1] = 10 + 2 = 12. By value, only copies change and A is unchanged.

More Compiler Design questions

Exams that ask this topic