ProgrammingVery hardTCS NQT · Infosys · Capgemini pattern

What will be the output of the following pseudocode? (Arrays are passed and assigned BY REFERENCE: b = a makes b point to the same array as a.)

What will be the output of the following pseudocode? (Arrays are passed and assigned BY REFERENCE: b = a makes b point to the same array as a.)
Integer a[5] = {46, 19, 41, 4, 14}
Integer[] b = a
Function modify(Integer[] x)
    for (each i from 0 to 4)
        x[i] = x[i] * 2
    end for
end Function
modify(b)
for (each i from 0 to 4)
    Print a[i]
end for
  1. A. 46, 19, 41, 4, 14
  2. B. 92, 38, 82, 8, 28
  3. C. 28, 8, 82, 38, 92
  4. D. 48, 21, 43, 6, 16

Correct answer: B. 92, 38, 82, 8, 28

Since b = a makes b an alias of the SAME array (pass/assign by reference), modifying b's elements through modify() also changes a. Every element is multiplied by 2, so a prints as [92, 38, 82, 8, 28]. Assuming a stays unchanged (as under pass-by-value) gives the trap [46, 19, 41, 4, 14].

More Pseudocode and Code Tracing questions

Exams that ask this topic