ProgrammingModerateTCS NQT · Wipro pattern

What is printed?

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

Correct answer: B. -4 1 -7

Floor division rounds toward negative infinity, so -7 // 2 is -4, and the remainder takes the divisor's sign, giving -7 % 2 = 1. The identity a == (a // b) * b + a % b holds, so the last value is -7.

More Python Programming questions

Exams that ask this topic