Java Operators & Expressions Quiz
40 medium-to-advanced questions Java operators & expressions that force deep thinking about arithmetic, relational, logical, assignment, and ternary operators — including precedence traps, short-circuit behavior, compound assignment quirks, and real-world expression bugs.
Question 1
What is the final value of result after this expression?
int a = 5;
int result = a++ + a + ++a * 2 - a--;Question 2
Why does this code NOT throw a NullPointerException?
String s = null;
if (s == null || s.length() > 0) {
System.out.println("Safe!");
}Question 3
What is the output of this tricky compound assignment?
byte b = 100;
b += 200;
System.out.println(b);Question 4
What is the value of x after this expression?
int x = 10;
x = x++ * 2 + --x;Question 5
Which operator has the HIGHEST precedence?
Question 6
What is printed by this bitwise and logical mix?
int a = 5; // binary 101
int b = 3; // binary 011
System.out.println(a & b);
System.out.println(a | b);
System.out.println(a ^ b);
System.out.println(~a);Question 7
What is the output of this short-circuit logic with side effects?
boolean x = false;
boolean y = true;
boolean result = x && increment() || y && increment();
System.out.println(result + " " + count);
// Assume: static int count = 0;
static boolean increment() { count++; return true; }Question 8
Which of these expressions requires parentheses to compile?
Question 9
What is the value of z after this complex expression?
int x = 8, y = 3;
int z = x++ - --y * 2 + x % y;Question 10
What is the output of this shift operator puzzle?
int x = -8;
System.out.println(x >> 2);
System.out.println(x >>> 2);Question 11
Why does this compile successfully?
short s = 100;
s *= 5;
System.out.println(s);Question 12
What is the result of this nested ternary?
int score = 85;
String grade = score >= 90 ? "A" :
score >= 80 ? "B" :
score >= 70 ? "C" : "F";
System.out.println(grade);Question 13
What is printed by this logical vs bitwise comparison?
boolean a = true, b = false;
System.out.println(a ^ b);
System.out.println(a & b);
System.out.println(a | b);Question 14
Which of these expressions will cause a compilation error?
Question 15
What is the output of this precedence puzzle?
System.out.println(5 + 3 * 4 % 2 - 1);Question 16
What is the value of i after this loop completes?
int i = 0;
while (i++ < 5) {
if (i % 2 == 0) continue;
System.out.print(i + " ");
}Question 17
What is printed by this deeply nested ternary?
int n = 15;
String result = n > 20 ? "High" : n > 10 ? "Medium" : n > 5 ? "Low" : "Very Low";
System.out.println(result);Question 18
Which statement about operator precedence is FALSE?
Question 19
What is the final value of x?
int x = 5;
x = x++ + x++ + ++x - --x + x--;Question 20
What is the output of this expression with multiple assignments?
int a, b, c;
a = b = c = 10 + 5;
System.out.println(a + " " + b + " " + c);Question 21
Which of these is equivalent to i++?
Question 22
What is printed?
int x = 10;
System.out.println(x > 10 ? "High" : x < 5 ? "Low" : "Medium");Question 23
What is the value of b after execution?
boolean a = true;
boolean b = !a-- || a++ && false;Question 24
Which operator is used for string concatenation?
Question 25
What is the output of this classic precedence trap?
System.out.println(10 + 20 + "30" + 40 + 50);Question 26
What is the final value of counter?
int counter = 0;
boolean flag = true;
if (flag = false | increment()) {
counter++;
}
System.out.println(counter);
static boolean increment() { counter++; return true; }Question 27
Which of these expressions has the same value as (a > b) ? a : b?
Question 28
What is printed?
int x = 5;
System.out.println(x >>>= 1);Question 29
Why is it dangerous to use == with floating-point numbers?
Question 30
What is the output of this final challenge?
int a = 10, b = 20, c = 15;
int result = ++a * 2 + (b-- > c ? --b : ++c) - a++;
System.out.println(result + ", a=" + a + ", b=" + b + ", c=" + c);Question 31
Which operator has the lowest precedence?
Question 32
What is the result?
System.out.println("Result: " + 10 + 20);Question 33
Which of these is NOT a valid operator in Java?
Question 34
What is the output?
int x = 1;
System.out.println(x << 1);Question 35
Why should you prefer && and || over & and | when working with boolean expressions?
Question 36
What is the value of result?
boolean flag = false;
int result = flag ? 100 : 200;
System.out.println(result);Question 37
Which assignment is valid without casting?
Question 38
What is printed?
System.out.println(5 <= 5 || 10 > 15 && 3 == 4);Question 39
What is the most important rule to remember about operator precedence?
Question 40
After mastering operators, what is the single biggest takeaway for writing robust Java code?
