ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
print(~5 == -6, ~(-5) == 4, (-5) >> 1 == -2, -5 // 2 == -3)
  1. A. True False False True
  2. B. True True False True
  3. C. True True True True
  4. D. False True False True

Correct answer: B. True True False True

~x equals -x - 1, so ~5 is -6 and ~(-5) is 4. Right shift on a negative number rounds toward negative infinity, so -5 >> 1 is -3, not -2. Floor division also gives -3.

More Python Programming questions

Exams that ask this topic