ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
print(-7.5 // 2, -7.5 % 2, 7 // -2.0, 7 % -2.0)
  1. A. -3.0 -1.5 -3.0 1.0
  2. B. -4 0.5 -4 -1
  3. C. -4.0 0.5 -3.0 1.0
  4. D. -4.0 0.5 -4.0 -1.0

Correct answer: D. -4.0 0.5 -4.0 -1.0

// floors toward minus infinity even for floats, and the result stays a float: -3.75 floors to -4.0, and 7 / -2.0 = -3.5 floors to -4.0. % is chosen so that a == (a // b) * b + a % b, which gives it the sign of the divisor: -7.5 - (-8.0) = 0.5 and 7 - 8.0 = -1.0. The -3.0 answers are C-style truncation.

More Python Programming questions

Exams that ask this topic