Bird
Raised Fist0

What will be the output of the following Python function when the input is 19?

easy📖 Theory Q3 of Q15
Bit Manipulation - Number of 1 Bits (Hamming Weight)
What will be the output of the following Python function when the input is 19?
def countSetBits(num: int) -> int:
    return bin(num).count('1')

print(countSetBits(19))
A4
B2
C3
D1
Step-by-Step Solution
Solution:
  1. Step 1: Convert 19 to binary

    19 in binary is 10011.
  2. Step 2: Count the number of 1 bits

    There are three 1s in 10011.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Counting '1's in '10011' gives 3 [OK]
Quick Trick: Count '1's in binary representation [OK]
Common Mistakes:
MISTAKES
  • Miscounting bits in binary representation
  • Confusing decimal with binary digits
Trap Explanation:
PITFALL
  • Counting decimal digits instead of binary bits looks plausible but is incorrect.
Interviewer Note:
CONTEXT
  • Tests understanding of binary representation and bit counting.
Master "Number of 1 Bits (Hamming Weight)" in Bit Manipulation

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bit Manipulation Quizzes