ProgrammingModerateAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer x = 15, y = 4, r = 0
while (x >= y)
    x = x - y
    r = r + 1
end while
Print r, x
  1. A. 3 4
  2. B. 3 3
  3. C. 4 -1
  4. D. 3 0

Correct answer: B. 3 3

Repeated subtraction computes quotient and remainder: 15 − 4 − 4 − 4 = 3, after 3 subtractions. So r = 15 / 4 = 3 and x = 15 mod 4 = 3. Using x > y would behave the same here but fail for x = 16 (remainder 4).

More Pseudocode and Code Tracing questions

Exams that ask this topic