ProgrammingVery hardProduct company OA · code-tracing level

What will be the output of the following pseudocode? (Integers are passed by value. An array parameter receives a reference to the caller's array; assigning a new array to the parameter changes only what the local name refers to.)

What will be the output of the following pseudocode? (Integers are passed by value. An array parameter receives a reference to the caller's array; assigning a new array to the parameter changes only what the local name refers to.)
Procedure change(Integer x, Integer arr[])
    x = x + 10
    arr[0] = arr[0] + x
    arr = new array {100, 200}
    arr[1] = 0
End Procedure

Integer v = 5
Integer a[2] = {1, 2}
change(v, a)
Print v, a[0], a[1]
  1. A. 5 16 2
  2. B. 15 16 0
  3. C. 5 16 0
  4. D. 5 100 0

Correct answer: A. 5 16 2

x is a copy, so v stays 5; inside, x becomes 15. arr refers to the caller's array, so arr[0] = 1 + 15 = 16 changes a[0]. Then arr is pointed at a brand-new array; arr[1] = 0 changes that new array, not a. So a = {16, 2}. '5 16 0' assumes the reassignment still affects the caller's array; '15 16 0' also treats x as passed by reference.

More Pseudocode and Code Tracing questions

Exams that ask this topic