ProgrammingHardTCS NQT · Accenture

What is printed?

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

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

With a negative divisor, % takes the divisor's sign, so 8 % -3 is -1 and -8 % -3 is -2. Floor division rounds toward negative infinity, so 8 // -3 is -3 and -8 // -3 is 2.

More Python Programming questions

Exams that ask this topic