ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
x = 0
x |= 1 << 3
x &= ~(1 << 0)
x ^= 0b111
print(x, x.bit_count(), (x >> 1) & -x)
  1. A. 15 3 1
  2. B. 15 4 1
  3. C. 15 4 7
  4. D. 8 4 1

Correct answer: B. 15 4 1

x becomes 8, then ~1 clears nothing, and XOR with 7 gives 15, which has four set bits. (15 >> 1) & -15 isolates the lowest set bit of 7, which is 1.

More Python Programming questions

Exams that ask this topic