Java Operators & Expressions Quiz

Java
0 Passed
0% acceptance

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.

40 Questions
~80 minutes
1

Question 1

What is the final value of result after this expression?

java
int a = 5;
int result = a++ + a + ++a * 2 - a--;
A
27
B
28
C
30
D
32
2

Question 2

Why does this code NOT throw a NullPointerException?

java
String s = null;
if (s == null || s.length() > 0) {
    System.out.println("Safe!");
}
A
Short-circuit || skips the second condition when first is true
B
String overrides == to handle null
C
The JVM optimizes null checks
D
It actually does throw NPE
3

Question 3

What is the output of this tricky compound assignment?

java
byte b = 100;
b += 200;
System.out.println(b);
A
44
B
300
C
Compilation error
D
-56
4

Question 4

What is the value of x after this expression?

java
int x = 10;
x = x++ * 2 + --x;
A
38
B
39
C
40
D
Undefined behavior
5

Question 5

Which operator has the HIGHEST precedence?

A
Postfix (x++, x--)
B
Unary (+x, -x, !x)
C
Multiplicative (*, /, %)
D
Additive (+, -)
6

Question 6

What is printed by this bitwise and logical mix?

java
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);
A
1\n7\n6\n-6
B
3\n5\n6\n-5
C
1\n7\n6\n-5
D
5\n3\n6\n-6
7

Question 7

What is the output of this short-circuit logic with side effects?

java
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; }
A
true 1
B
true 2
C
false 0
D
true 0
8

Question 8

Which of these expressions requires parentheses to compile?

A
a = b ? c = 10 : d = 20
B
a = b ? c : d = 20
C
a = (b ? c : d) = 20
D
a = b ? c : (d = 20)
9

Question 9

What is the value of z after this complex expression?

java
int x = 8, y = 3;
int z = x++ - --y * 2 + x % y;
A
10
B
11
C
12
D
13
10

Question 10

What is the output of this shift operator puzzle?

java
int x = -8;
System.out.println(x >> 2);
System.out.println(x >>> 2);
A
-2\n1073741822
B
-2\n-2
C
1073741822\n-2
D
-8\n-8
11

Question 11

Why does this compile successfully?

java
short s = 100;
s *= 5;
System.out.println(s);
A
Compound assignment with *= performs automatic cast back to short
B
short can hold 500
C
All arithmetic is done in int
D
It doesn't compile
12

Question 12

What is the result of this nested ternary?

java
int score = 85;
String grade = score >= 90 ? "A" :
              score >= 80 ? "B" :
              score >= 70 ? "C" : "F";
System.out.println(grade);
A
B
B
A
C
C
D
F
13

Question 13

What is printed by this logical vs bitwise comparison?

java
boolean a = true, b = false;
System.out.println(a ^ b);
System.out.println(a & b);
System.out.println(a | b);
A
true\nfalse\ntrue
B
1\n0\n1
C
true\ntrue\ntrue
D
Compilation error
14

Question 14

Which of these expressions will cause a compilation error?

A
int x = 10; x += 5.5;
B
int x = 10; x = x + 5.5;
C
double d = 10; d += 5;
D
float f = 10; f += 5;
15

Question 15

What is the output of this precedence puzzle?

java
System.out.println(5 + 3 * 4 % 2 - 1);
A
4
B
7
C
16
D
6
16

Question 16

What is the value of i after this loop completes?

java
int i = 0;
while (i++ < 5) {
    if (i % 2 == 0) continue;
    System.out.print(i + " ");
}
A
6
B
5
C
7
D
Infinite loop
17

Question 17

What is printed by this deeply nested ternary?

java
int n = 15;
String result = n > 20 ? "High" : n > 10 ? "Medium" : n > 5 ? "Low" : "Very Low";
System.out.println(result);
A
Medium
B
Low
C
High
D
Very Low
18

Question 18

Which statement about operator precedence is FALSE?

A
&& has higher precedence than ||
B
== has higher precedence than &
C
* has higher precedence than +
D
? : has higher precedence than =
19

Question 19

What is the final value of x?

java
int x = 5;
x = x++ + x++ + ++x - --x + x--;
A
30
B
31
C
32
D
Undefined
20

Question 20

What is the output of this expression with multiple assignments?

java
int a, b, c;
a = b = c = 10 + 5;
System.out.println(a + " " + b + " " + c);
A
15 15 15
B
10 5 15
C
15 10 5
D
Compilation error
21

Question 21

Which of these is equivalent to i++?

A
i = i + 1
B
++i
C
i += 1
D
Both A and C
22

Question 22

What is printed?

java
int x = 10;
System.out.println(x > 10 ? "High" : x < 5 ? "Low" : "Medium");
A
Medium
B
High
C
Low
D
Compilation error
23

Question 23

What is the value of b after execution?

java
boolean a = true;
boolean b = !a-- || a++ && false;
A
true
B
false
C
Compilation error
D
Undefined
24

Question 24

Which operator is used for string concatenation?

A
+
B
"+" and "+="
C
++
D
&
25

Question 25

What is the output of this classic precedence trap?

java
System.out.println(10 + 20 + "30" + 40 + 50);
A
30304050
B
1203050
C
1303050
D
100
26

Question 26

What is the final value of counter?

java
int counter = 0;
boolean flag = true;
if (flag = false | increment()) {
    counter++;
}
System.out.println(counter);

static boolean increment() { counter++; return true; }
A
1
B
2
C
0
D
Compilation error
27

Question 27

Which of these expressions has the same value as (a > b) ? a : b?

A
Math.max(a, b)
B
a > b ? a : b
C
Both A and B
D
Neither
28

Question 28

What is printed?

java
int x = 5;
System.out.println(x >>>= 1);
A
2
B
5
C
10
D
0
29

Question 29

Why is it dangerous to use == with floating-point numbers?

A
Because of precision errors in IEEE 754 representation
B
Because floating-point is always exact
C
Because == is not allowed on double
D
Only when using float, not double
30

Question 30

What is the output of this final challenge?

java
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);
A
40, a=12, b=18, c=15
B
41, a=12, b=19, c=15
C
39, a=11, b=18, c=16
D
42, a=12, b=18, c=16
31

Question 31

Which operator has the lowest precedence?

A
? :
B
=
C
||
D
All assignment operators
32

Question 32

What is the result?

java
System.out.println("Result: " + 10 + 20);
A
Result: 1020
B
Result: 30
C
Compilation error
D
Result: 10 20
33

Question 33

Which of these is NOT a valid operator in Java?

A
***
B
instanceof
C
->
D
>>>
34

Question 34

What is the output?

java
int x = 1;
System.out.println(x << 1);
A
2
B
-2
C
1
D
0
35

Question 35

Why should you prefer && and || over & and | when working with boolean expressions?

A
They provide short-circuit evaluation and better performance/safety
B
They are faster to type
C
They work with integers
D
No difference
36

Question 36

What is the value of result?

java
boolean flag = false;
int result = flag ? 100 : 200;
System.out.println(result);
A
200
B
100
C
0
D
Compilation error
37

Question 37

Which assignment is valid without casting?

A
byte b = 100; b = b + 50;
B
byte b = 100; b += 50;
C
Both
D
Neither
38

Question 38

What is printed?

java
System.out.println(5 <= 5 || 10 > 15 && 3 == 4);
A
true
B
false
C
Compilation error
D
0
39

Question 39

What is the most important rule to remember about operator precedence?

A
When in doubt, use parentheses — clarity over cleverness
B
Memorize the full 16-level table
C
All operators have the same precedence
D
Only + and - matter
40

Question 40

After mastering operators, what is the single biggest takeaway for writing robust Java code?

A
Never sacrifice readability for brevity — the JVM doesn’t care about cleverness, but your teammates do
B
Always use the most complex expression possible
C
Operator precedence is unimportant
D
Only use + and = operators

QUIZZES IN Java