ProgrammingHardTCS NQT · Accenture

What is printed?

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

Correct answer: B. -4 -4 -1 (3, -1)

Floor division gives 7 // -2 = -4 because the quotient -3.5 is rounded down. The remainder 7 % -2 must have the divisor's sign, so it is -1, and divmod(-7, -2) is (3, -1) since 3 * -2 + -1 = -7.

More Python Programming questions

Exams that ask this topic