Java 21 Certification Guide - Part 5
Welcome to the fifth article in my series on preparing for the Java 21 certification.
Over the next several posts, we’ll walk through the most important language changes, APIs, and tricky details you’ll need to know for the exam.
In this article, we’ll focus on bitwise operations and binary number representation. These concepts aren’t something most developers use in their daily work, so they can feel a bit tricky.
To make things practical, we’ll analyze a question in the style of the certification exam.
❓ Question
What is the output for this code?
public static void main(String[] args) {
int a = 12; // 1100 in binary
int b = 5; // 0101 in binary
int result1 = a & b;
int result2 = a | b;
int result3 = a ^ b;
int result4 = ~a;
int result5 = a ^ result4;
System.out.println(result1 + ", " + result2 + ", " + result3 + ", " + result4 + ", " + result5);
}
A) 4, 13, 9, -13, -1
B) 4, 13, 9, -13, 25
C) 0, 17, 7, -12, -1
D) 5, 12, 7, -13, 0
E) 4, 12, 9, -13, -1
F) 4, 13, 7, -12, -1
🧠 Explaining the problem:
result1 = a & b
1100 & 0101 = 0100
→ 4result2 = a | b
1100 | 0101 = 1101
→ 13result3 = a ^ b
1100 ^ 0101 = 1001
→ 9result4 = ~a
The~
operator flips all bits ofa
.~12
→ -13 (in two’s complement representation).result5 = a ^ result4
12 ^ -13
=-1
(because each bit ofa
and~a
are complements, producing all1
s in binary).
✅ Correct Answer
A) 4, 13, 9, -13, -1
Did you get it right? Don’t worry if you missed some; The goal here is to expose you to several tricky details that often show up in the Java 21 certification exam.
⚡ Next in this series: we’ll dive into Modules
📬 Stay tuned:
If you don’t want to miss the upcoming parts of this Java 21 Certification series, hit the Subscribe button below.