0
0
DSA Pythonprogramming~10 mins

Check if Number is Even or Odd Using Bits in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a number is even using bitwise AND.

DSA Python
def is_even(num):
    return (num & [1]) == 0

print(is_even(4))  # True
print(is_even(7))  # False
Drag options to blanks, or click blank then click option'
A1
B0
C2
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as mask always returns 0, which is incorrect.
Using the number itself as mask causes errors.
2fill in blank
medium

Complete the code to check if a number is odd using bitwise AND.

DSA Python
def is_odd(num):
    return (num & [1]) == 1

print(is_odd(5))  # True
print(is_odd(8))  # False
Drag options to blanks, or click blank then click option'
A0
Bnum
C2
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as mask always returns 0.
Using 2 as mask checks wrong bit.
3fill in blank
hard

Fix the error in the code to correctly check if a number is even using bits.

DSA Python
def check_even(num):
    if (num & [1]) == 0:
        return True
    else:
        return False

print(check_even(10))  # Should be True
print(check_even(7))   # Should be False
Drag options to blanks, or click blank then click option'
A1
B2
C0
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if (num & 1) == 1 means odd, not even.
Using wrong mask value.
4fill in blank
hard

Fill both blanks to create a function that returns 'Even' or 'Odd' using bitwise operation.

DSA Python
def even_or_odd(num):
    if (num & [1]) == [2]:
        return 'Even'
    else:
        return 'Odd'

print(even_or_odd(12))  # Even
print(even_or_odd(15))  # Odd
Drag options to blanks, or click blank then click option'
A1
B0
C2
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping mask and comparison values.
Using incorrect mask value.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that labels numbers as 'Even' or 'Odd' using bits.

DSA Python
numbers = [1, 2, 3, 4, 5]
result = {num: 'Even' if (num & [1]) == [2] else 'Odd' for [3] in numbers}
print(result)
Drag options to blanks, or click blank then click option'
A1
B0
Cnum
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong iteration variable.
Incorrect mask or comparison values.