ProgrammingHardInfosys · Capgemini pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
// C-style integer arithmetic: / truncates toward zero, mod keeps the dividend's sign
Integer x = 7, y = 3
Print (x / y) * y + (x mod y), (-x) / y, (-x) mod y
  1. A. 7 -3 2
  2. B. 6 -2 -1
  3. C. 7 -2 2
  4. D. 7 -2 -1

Correct answer: D. 7 -2 -1

(x / y)·y + x mod y always rebuilds x: 2·3 + 1 = 7. With truncation, −7 / 3 = −2 and −7 mod 3 = −1 (so that −2·3 + (−1) = −7). Floor-based languages such as Python give −3 and 2 instead.

More Pseudocode and Code Tracing questions

Exams that ask this topic