Challenge - 5 Problems
Bitwise Even-Odd Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code snippet?
Given the code below, what will be printed when the input number is 10?
DSA C
#include <stdio.h> int main() { int num = 10; if (num & 1) { printf("Odd\n"); } else { printf("Even\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check the least significant bit of the number using bitwise AND with 1.
✗ Incorrect
The bitwise AND operation num & 1 checks the least significant bit. For 10 (binary 1010), the last bit is 0, so it is even.
❓ Predict Output
intermediate2:00remaining
What is the output when num = 7?
Analyze the code below and determine the output when num is 7.
DSA C
#include <stdio.h> int main() { int num = 7; if ((num & 1) == 1) { printf("Odd\n"); } else { printf("Even\n"); } return 0; }
Attempts:
2 left
💡 Hint
The expression (num & 1) checks if the last bit is set.
✗ Incorrect
7 in binary is 111, so the last bit is 1, making it odd.
🧠 Conceptual
advanced2:00remaining
Why does using bitwise AND with 1 check if a number is even or odd?
Select the best explanation for why the expression (num & 1) can determine if num is even or odd.
Attempts:
2 left
💡 Hint
Think about the binary representation of even and odd numbers.
✗ Incorrect
In binary, odd numbers always end with 1, so ANDing with 1 isolates that bit to check parity.
❓ Predict Output
advanced2:00remaining
What is the output of this code snippet?
What will be printed when the input number is -3?
DSA C
#include <stdio.h> int main() { int num = -3; if (num & 1) { printf("Odd\n"); } else { printf("Even\n"); } return 0; }
Attempts:
2 left
💡 Hint
Consider how negative numbers are represented in two's complement.
✗ Incorrect
In two's complement, negative odd numbers still have the least significant bit set to 1, so the output is Odd.
🔧 Debug
expert2:00remaining
What error does this code produce?
Identify the error in the following code snippet.
DSA C
#include <stdio.h> int main() { int num = 4; if (num & 1 == 1) { printf("Odd\n"); } else { printf("Even\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check operator precedence between & and ==.
✗ Incorrect
The expression num & 1 == 1 is parsed as num & (1 == 1), which is num & 1, so the condition is true for any num with last bit 1. For num=4 (binary 100), num & 1 is 0, so condition false, prints Even. But for num=5, it prints Odd. The problem is subtle: the condition is correct but can be confusing without parentheses.
