ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: D. -4 -4 1 -1

Floor division rounds toward negative infinity, so -7 // 2 is -4 and 7 // -2 is -4. The remainder takes the sign of the divisor: -7 % 2 is 1 and 7 % -2 is -1.

More Python Programming questions

Exams that ask this topic