Data Structures & AlgorithmsHardTCS NQT Digital · language semantics

Problem: What does the following print?

Problem: What does the following print?
print(-7 // 2, -7 % 2, 7 // -2, int(-7 / 2))
  1. A. -3 2 -3 -3
  2. B. -5 1 -4 -3
  3. C. -4 1 -4 -3
  4. D. -3 1 -3 -3

Correct answer: C. -4 1 -4 -3

Python // floors toward negative infinity, so -7 // 2 = -4 and -7 % 2 = 1, while int() truncates toward zero, giving -3. Languages such as C truncate, which is the trap.

More Coding Interview Problems questions

Exams that ask this topic