Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: What does this Python snippet print? print(-7 // 2, -7 % 3, 7 // -2)

  1. A. -3 2 -3
  2. B. -4 -1 -3
  3. C. -4 2 -4
  4. D. -3 -1 -4

Correct answer: C. -4 2 -4

Python's floor division rounds toward negative infinity and the modulo takes the sign of the divisor. Truncating toward zero (as in C) gives different values for the negative operands.

More Coding Interview Problems questions

Exams that ask this topic