ProgrammingVery hardProduct company OA

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

What will be the output of the following pseudocode? ('ref' parameters are passed by reference; others by value.)
Procedure p(ref Integer a, Integer b)
    a = a + b
    b = a - b
End Procedure
Integer x = 3, y = 4
p(x, y)
p(y, x)
Print x, y
  1. A. 7 11
  2. B. 4 3
  3. C. 7 4
  4. D. 11 7

Correct answer: A. 7 11

p(x, y): x (by reference) becomes 3 + 4 = 7; the local copy b changes, but y stays 4. p(y, x): now y is the reference: y = 4 + 7 = 11; again only the local b changes, so x stays 7. Output 7 11. Treating both parameters as references turns p into a partial swap and gives 4 3-style results.

More Pseudocode and Code Tracing questions

Exams that ask this topic