ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
print(bin(29 ^ 15).count('1'))
  1. A. 3
  2. B. 1
  3. C. 2
  4. D. 4

Correct answer: C. 2

XOR marks the bit positions where two numbers differ, so the number of ones in 29 ^ 15 is the number of bit flips needed. 29 is 11101 and 15 is 01111, and they differ in two positions.

More Python Programming questions

Exams that ask this topic