ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
print(bin(10).count('1'), (10).bit_count(), (-10).bit_count())
  1. A. 2 2 -10
  2. B. 2 2 3
  3. C. 2 2 0
  4. D. 2 2 2

Correct answer: D. 2 2 2

int.bit_count counts the 1-bits of the absolute value, so -10 has the same count as 10, which is 1010 in binary and gives 2.

More Python Programming questions

Exams that ask this topic