ProgrammingHardTCS NQT · Wipro pattern

What is printed?

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

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

Python floors toward minus infinity, so -7 // 2 is -4. The remainder takes the sign of the divisor: -7 % 2 is 1 and 7 % -2 is -1. divmod follows the same rule.

More Python Programming questions

Exams that ask this topic