What is the output of the following Python code? print(-7 // 2, -7 % 2)
Correct answer B. -4 1
Explanation
The // operator floors the result towards negative infinity, so -3.5 becomes -4, not -3 as it would with truncation in C or Java. Python keeps the identity (a // b) * b + (a % b) == a, so the remainder must be -7 - (-8) = 1. The result of % always takes the sign of the divisor, which is positive here.
Answer checked How we write and check questions
Discussion