ProgrammingHardTCS NQT · Infosys · Capgemini

What will be the output of the following pseudocode? (Parameters marked ref are passed by reference; the others by value.)

What will be the output of the following pseudocode? (Parameters marked ref are passed by reference; the others by value.)
Procedure shift(ref a, ref b, c)
    Integer t = a
    a = b + c
    b = t * c
    c = a - b
End Procedure
Integer x = 3, y = 4, z = 5
shift(x, y, z)
shift(y, x, z)
Print x, y, z
  1. A. 75 14 5
  2. B. -90 3 93
  3. C. 13 100 5
  4. D. 3 4 5

Correct answer: A. 75 14 5

First call: a=x, b=y: x = 4+5 = 9, y = 3*5 = 15 (c changes only the local copy). Second call: a=y, b=x, t = 15: y = 9+5 = 14, x = 15*5 = 75. z stays 5 throughout because c is by value.

More Pseudocode and Code Tracing questions

Exams that ask this topic