ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
print(bin(-1 & 0b111), (-1) & 0xF == 15, -16 >> 2, -15 >> 2)
  1. A. 0b111 False -4 -4
  2. B. 0b111 True -4 -4
  3. C. 0b111 True -3 -4
  4. D. 0b11 True -4 -4

Correct answer: B. 0b111 True -4 -4

-1 has all bits set, so masking with 0b111 gives 7 (0b111). The & operator binds tighter than ==, and right shifts of negative ints floor, so -15 >> 2 is -4.

More Python Programming questions

Exams that ask this topic