Data Structures & AlgorithmsHardTCS NQT Digital · Python semantics

Problem: What does the following print?

Problem: What does the following print?
print(2 ** -1, 7 % -3)
  1. A. 0.25 2
  2. B. 0.5 -2
  3. C. 0 -2
  4. D. 0.5 -1

Correct answer: B. 0.5 -2

Negative exponents give floats, so 2 ** -1 = 0.5. Python's modulo takes the sign of the divisor, so 7 % -3 = -2.

More Coding Interview Problems questions

Exams that ask this topic