ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
print(-7 % 3, 7 % -3, -7 // 3, 7 // -3)
  1. A. 2 -2 -3 -3
  2. B. 2 -2 -2 -3
  3. C. 1 2 -3 -3
  4. D. 2 -2 -3 -2

Correct answer: A. 2 -2 -3 -3

Python's % takes the sign of the divisor and // floors toward minus infinity, so -7 % 3 = 2, 7 % -3 = -2 and -7 // 3 = -3. A C-style truncating division would print -2 for -7 // 3, which is the trap.

More Python Programming questions

Exams that ask this topic