ProgrammingHardTCS NQT Advanced · Accenture

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Procedure upd(Integer arr[], Integer n)     // arrays are passed by reference
    arr[0] = arr[0] + n
    n = n * 2
    arr[1] = arr[1] + n
End Procedure
Integer a[2] = {1, 1}, k = 3
upd(a, k)
upd(a, k)
Print a[0], a[1], k
  1. A. 7 13 12
  2. B. 4 7 3
  3. C. 7 25 12
  4. D. 7 13 3

Correct answer: D. 7 13 3

The array is shared, but n is a copy, so k stays 3. Each call adds 3 to a[0] and 6 to a[1]: after two calls a = {7, 13}. Output 7 13 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic