Challenge - 5 Problems
Bitwise Even-Odd Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Java code using bitwise AND to check even or odd?
Consider the following Java code snippet that checks if a number is even or odd using bitwise operations. What will it print when n = 7?
DSA Java
int n = 7; if ((n & 1) == 0) { System.out.println("Even"); } else { System.out.println("Odd"); }
Attempts:
2 left
💡 Hint
The bitwise AND with 1 checks the least significant bit of the number.
✗ Incorrect
The expression (n & 1) extracts the least significant bit. If it is 0, the number is even; if 1, the number is odd. Since 7 in binary ends with 1, it is odd.
❓ Predict Output
intermediateWhat is the output when checking if 12 is even or odd using bitwise AND?
Given the code below, what will be printed when n = 12?
DSA Java
int n = 12; if ((n & 1) == 0) { System.out.println("Even"); } else { System.out.println("Odd"); }
Attempts:
2 left
💡 Hint
Check the least significant bit of 12 in binary.
✗ Incorrect
12 in binary ends with 0, so (12 & 1) == 0 is true, printing "Even".
🔧 Debug
advancedWhy does this code fail to correctly identify odd numbers?
This Java code attempts to print "Odd" or "Even" but does not compile. Identify the bug.
DSA Java
int n = 5; if ((n & 1) == 0) { System.out.println("Even"); } else { System.out.println("Odd"); }
Attempts:
2 left
💡 Hint
Check the condition inside the if statement carefully.
✗ Incorrect
Using '=' assigns 0 to (n & 1), which is invalid syntax. The comparison operator '==' is needed.
❓ Predict Output
advancedWhat is the output of this Java code checking multiple numbers for even or odd?
What will be printed by the following code?
DSA Java
int[] nums = {2, 3, 4}; for (int n : nums) { if ((n & 1) == 0) { System.out.print("E"); } else { System.out.print("O"); } }
Attempts:
2 left
💡 Hint
Check each number's least significant bit and print 'E' for even, 'O' for odd.
✗ Incorrect
2 is even (E), 3 is odd (O), 4 is even (E), so output is "EOE".
🧠 Conceptual
expertWhy is bitwise AND with 1 a preferred method to check even or odd in low-level programming?
Select the best reason why using (n & 1) is often preferred over (n % 2) to check if a number is even or odd in performance-critical code.
Attempts:
2 left
💡 Hint
Think about how computers handle division vs bitwise operations.
✗ Incorrect
Bitwise AND with 1 is a simple, fast operation that checks the last bit, avoiding costly division used by modulo.
