ProgrammingVery hardTCS NQT · Infosys · Cognizant GenC

Integers are passed by value and arrays by reference (the reference itself is copied). g(a, x): a[0] = a[0] + x x = x * 2 a = [9, 9] a[1] = x return x A = [1, 2] x = 3 r = g(A, x) print A[0], A[1], x, r What is printed?

  1. A. 4 6 3 6
  2. B. 4 2 6 6
  3. C. 4 2 3 6
  4. D. 1 2 3 6

Correct answer: C. 4 2 3 6

a[0] += x updates the caller's array to [4, 2]. Then x = x*2 changes only the local copy (6). After a = [9, 9] the parameter points to a new array, so a[1] = x does not touch A. The caller prints 4 2 3 and r = 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic