Bird
Raised Fist0

Given the following code, what is the output of isPowerOfTwo(16) and isPowerOfTwo(218)?

easy🧾 Code Trace Q12 of Q15
Bit Manipulation - Power of Two
Given the following code, what is the output of isPowerOfTwo(16) and isPowerOfTwo(218)?
def isPowerOfTwo(n: int) -> bool:
    if n <= 0:
        return False
    return bin(n).count('1') == 1

print(isPowerOfTwo(16))
print(isPowerOfTwo(218))
ATrue and False
BFalse and True
CTrue and True
DFalse and False
Step-by-Step Solution
Solution:
  1. Step 1: Trace isPowerOfTwo(16)

    16 in binary is 0b10000, which has exactly one '1' bit, so returns True.
  2. Step 2: Trace isPowerOfTwo(218)

    218 in binary is 0b11011010, which has multiple '1' bits, so returns False.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Count of '1's in bin(16) == 1, in bin(218) > 1 [OK]
Quick Trick: Power of two has exactly one set bit [OK]
Common Mistakes:
MISTAKES
  • Miscounting bits in binary representation
  • Confusing zero or negative inputs
Trap Explanation:
PITFALL
  • Some candidates confuse binary count or forget to handle zero/negative inputs.
Interviewer Note:
CONTEXT
  • Checks ability to mentally execute bit counting code correctly.
Master "Power of Two" 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