Java Operators Quiz – 30 MCQ Questions with Answers

Java operators are special symbols used to perform operations on variables and values in a Java program. They play an important role in decision-making, calculations, comparisons, logical operations, and data manipulation. It is essential to understand operator concepts for beginners, or programmers, because operators are used in almost every Java application.

Therefore, we have compiled the top 30 quiz questions based on Java operators from basic to advanced levels. These quiz questions will not only enhance your knowledge but also improve your problem-solving skills.

There is no time limit. At the end of the quiz, you will get the scorecard, which will show the performance in the quiz.

Let’s start the MCQ quiz and check how well you know all types of operators of Java programming! Can you score 30/30?

Which operator is used for addition in Java?
A. +
B. –
C. *
D. /
+
The + operator is an arithmetic operator used to add two numbers in Java.

What is the output of:


int x = 10 % 3;
A. 3
B. 1
C. 0
D. 2
1
  • The modulus/remainder operator % returns the remainder after division.
  • 10 ÷ 3 = 3 remainder 1, so 10 % 3 = 1.
Which of the following operators is used to check equality in Java?
A. =
B. ===
C. ==
D. !=
==
  • == is the equality operator, which is used to compare values or references.
  • = is the assignment operator.
  • === does not exist in Java.
  • != is the not-equal operator.
What does the ++ operator do when used as x++ (post-increment)?
A. Increments x before use
B. Decrements x
C. Returns current value of x, then increments x by 1
D. Multiplies x by 2
Returns current value of x, then increments x by 1
  • x++ is the post-increment operator. It returns the current value of x first, then increments x by 1.
  • In contrast, ++x (pre-increment) increments first, then returns the new value.
What is the type of the result of 5 + 3.0 in Java?
A. int
B. float
C. double
D. long
double
  • When int and double are combined, Java auto-promotes (widens) the int to double.
  • 3.0 is a double literal, so 5 + 3.0 results in double (8.0).
  • This is called numeric type promotion.
What does a ^= b mean?
A. a = a raised to the power b
B. a = a XOR b (bitwise)
C. a = a AND b
D. a = a OR b
a = a XOR b (bitwise)
  • ^= is the compound bitwise XOR assignment operator.
  • a ^= b is equivalent to a = a ^ b, where ^ is the bitwise XOR operator.
  • Note: ^ is not exponentiation — Java uses Math.pow() for that.

What is the output of the following code?

System.out.println(true && false);
A. true
B. false
C. 0
D. Error
false
&& is the logical AND operator. It returns true only if both conditions are true.

What is the output of the following code snippet?

int a = 5;
System.out.println(a++ + ++a);
A. 10
B. 11
C. 12
D. 13
12
  • Initially, a = 5.
  • a++ returns 5 (post-increment), then a becomes 6.
  • ++a pre-increments a to 7, then returns 7.
  • So the expression evaluates to 5 + 7 = 12.
What does the >>> operator do in Java?
A. Arithmetic right shift
B. Left shift
C. Unsigned right shift
D. Signed right shift
Unsigned right shift
  • >>> is the unsigned right shift operator, which shifts bits right and always fills the leftmost bits with 0, regardless of the sign.
  • >> is the signed right shift operator, which fills with the sign bit.
  • For negative numbers, >>> gives a very different result than >>.
Which of the following operators performs XOR operation in Java?
A. &
B. |
C. ^
D. ~
^
The ^ operator performs bitwise XOR. It returns 1 only when the corresponding bits are different.

What is the output of the following program?


int a = 4;
int b = 2;
System.out.print(a << b); 
System.out.print(" "); 
System.out.print(a >> 2);
A. 16 1
B. 8 2
C. 4 1
D. 16 2
16 1
  • This program uses two bitwise shift operators: Left Shift (<<) and Right Shift (>>).
  • Binary of 4 is 00000100. After shifting left by 2 positions: 00010000 = 16 in decimal. Shortcut: 4 × 2² = 16.
  • Binary of 4 is 00000100. After shifting right by 2 positions: 00000001 = 1 in decimal. Shortcut: 4 ÷ 2² = 1.

What will be the output of the following Java program?

String s = "Java";
System.out.println(s instanceof String);
A. true
B. false
C. Error
D. null
true
The instanceof operator checks whether an object belongs to a class. Since s is a String object, the result is true.

Predict the output of the following program.

int a = 5;
int b = 10;
int c = (a > b) ? a : b;
System.out.println(c);
A. 5
B. 10
C. 15
D. Error
10
  • Condition: a > b means 5 > 10, which is false.
  • Since the condition is false, the second value b (10) is selected and assigned to c.

What is the result of the following Java code?

System.out.println(~5);
A. -5
B. -6
C. 5
D. 4
-6
  • ~ is the bitwise complement (NOT) operator.
  • In Java’s two’s complement representation, ~n = -(n+1).
  • So ~5 = -(5+1) = -6. It flips all the bits of the integer.

What is the output of the following code snippet?

int x = 10;
System.out.println(x += 2 * 3);
A. 16
B. 36
C. 60
D. 12
16
  • Multiplication has higher precedence than addition.
  • So, 2 * 3 = 6 first, then 10 + 6 = 16.

What is the output of the following Java code snippet?

System.out.print(10 + 20 + "Java");
System.out.print(" ");
System.out.print("Java" + 20 + 10);
A. 1020Java Java2010
B. 30Java Java30
C. 1020Java Java30
D. 30Java Java2010
30Java Java2010
  • In 10 + 20 + “Java”: both 10 and 20 are integers, so Java performs addition first: 10 + 20 = 30. Then, since one operand is a String, it performs string concatenation: 30 + “Java” = “30Java”.
  • In “Java” + 20 + 10: since the first operand is a String, all subsequent + operators perform concatenation from left to right: “Java” + 20 = “Java20”, then “Java20” + 10 = “Java2010”.

What is the output of the Java code snippet?

int x = 5;
System.out.println(x++ * ++x);
A. 25
B. 30
C. 35
D. 42
35
  • x++ returns 5 (post-increment), so x becomes 6.
  • ++x pre-increments x to 7 and returns 7.
  • Expression evaluates to 5 × 7 = 35.

What is the output of the code after it is executed?

int a = 10, b = 20;
System.out.println(a > b ? "A" : b > a ? "B" : "Equal");
A. A
B. B
C. Equal
D. Compile error
B
  • Ternary operators are right-associative.
  • a > b (10 > 20) is false, so we evaluate b > a (20 > 10) which is true, returning “B”.
  • The expression is equivalent to: if(a>b) “A” else if(b>a) “B” else “Equal”.

What is the value of the following expression in Java?

(5 + (!2 + 8) * 3 - 3 % 2) / 2
A. 2
B. 11
C. 16
D. None of the above
None of the above
  • In Java, the ! operator is a logical NOT operator that works only with boolean values (true or false).
  • The expression !2 attempts to apply ! to an integer, which is invalid.
  • Therefore, Java produces a compile-time error.
What does -1 >>> 1 evaluate to in Java?
A. -1
B. 0
C. Integer.MAX_VALUE
D. -2
Integer.MAX_VALUE
  • -1 in binary (32-bit two’s complement) is all 1s: 11111111 11111111 11111111 11111111.
  • The unsigned right shift >>> fills with 0s from the left, giving: 01111111 11111111 11111111 11111111.
  • This equals Integer.MAX_VALUE = 2147483647.

What will be printed as the output of the code snippet?

int i = 0;
System.out.println(i++ == 0 && ++i == 2);
A. true
B. false
C. Compile error
D. Runtime error
true
  • i = 0. i++ == 0: returns 0 (post-increment), 0 == 0 is true, i becomes 1.
  • Since the left side is true, && evaluates the right side: ++i increments i to 2, and 2 == 2 is true.
  • true && true = true. The final value of i is 2.
What does System.out.println(‘a’ + 1) print?
A. a1
B. b
C. 98
D. Compile error
98
  • In Java, char participates in arithmetic and is auto-promoted to int.
  • The char ‘a’ has ASCII/Unicode value 97. So 97 + 1 = 98.
  • Since there is no char context, 98 (int) is printed. To get ‘b’, use System.out.println((char)(‘a’ + 1)).
What is the precedence order (highest to lowest) among: +, *, ++, ()?
A. () > ++ > * > +
B. ++ > () > * > +
C. * > + > () > ++
D. () > * > + > ++
() > ++ > * > +
  • Java operator precedence (high to low): parentheses () have the highest precedence.
  • Then unary ++ (postfix/prefix), then * (multiplicative), then + (additive).
  • Understanding precedence prevents subtle bugs in complex expressions.
What is the output of System.out.println(2 + 0x10)?
A. 210
B. 18
C. Compile error
D. 0x12
18
  • 0x10 is a hexadecimal integer literal in Java representing 16 (1×16 + 0×1).
  • So 2 + 16 = 18.

What will be the result of the following Java code snippet?

int a = 5, b = 10;
a = b + (b = a);
System.out.println(a + " " + b);
A. 15 5
B. 10 5
C. 15 10
D. 5 10
15 5
  • Java evaluates expressions left-to-right. The value of b is fetched first as 10.
  • Then (b = a) executes, assigning 5 to b and returning 5.
  • So a = 10 + 5 = 15. Final values: a = 15, b = 5.
  • This is a classic swap-attempt using operators.
Which statement about the logical operators & and && is true?
A. The & and && operators are interchangeable, always producing the same results at runtime.
B. The & operator always evaluates both operands, while the && operator may only evaluate the left operand.
C. Both expressions evaluate to true if either operand is true.
D. The & operator always evaluates both operands, while the && operator may only evaluate the right operand.
The & operator always evaluates both operands, while the && operator may only evaluate the left operand.
  • & is the non-short-circuit AND operator — it always evaluates both operands regardless of the left side’s result.
  • && is the short-circuit AND operator — if the left operand is false, it skips evaluating the right operand entirely.

What is printed as the output of the code?

int i = 1;
System.out.println(i + i++ + ++i);
A. 5
B. 6
C. 7
D. 4
5
  • Left-to-right evaluation: first i reads 1 (i is still 1).
  • i++ reads 1 (post-increment), i becomes 2.
  • ++i pre-increments i to 3, returns 3.
  • Sum = 1 + 1 + 3 = 5.

What will be printed as the output of the code snippet?

System.out.print(6 & 3);
System.out.print(" ");
System.out.print(5 | 3);
System.out.print(" ");
System.out.print(5 ^ 3);
A. 6 5 5
B. 3 3 7
C. 7 15 2
D. 2 7 6
2 7 6
  • Bitwise AND: 6 = 0110, 3 = 0011. AND: 0110 & 0011 = 0010 = 2. Each bit is 1 only if both corresponding bits are 1.
  • Bitwise OR: 5 = 0101, 3 = 0011. OR: 0101 | 0011 = 0111 = 7. Each bit is 1 if at least one of the corresponding bits is 1.
  • Bitwise XOR: 5 = 0101, 3 = 0011. XOR: 0101 ^ 0011 = 0110 = 6. Each bit is 1 only if the corresponding bits are different.

What happens when the following Java code is compiled and executed?

int time = 11;
int day = 4;
String dinner = time > 10 ? day ? "Takeout" : "Salad" : "Leftovers";
System.out.println(dinner);
A. Takeout
B. Salad
C. The code does not compile, but it would compile if parentheses were added correctly.
D. Leftovers
The code does not compile, but it would compile if parentheses were added correctly.
  • The first condition time > 10 is valid because it returns a boolean value.
  • However, the nested condition day ? “Takeout” : “Salad” uses day, which is an integer — not a boolean expression.
  • In Java, the ternary operator’s condition must always be boolean, so the compiler produces an error.
Which expression correctly swaps two integers a and b without using a temporary variable?
A. a = a ^ b ^ a;
B. a = a ^ b; a = b ^ a;
C. b ^= a; a ^= a; a ^= b;
D. a ^= b; b ^= a; a ^= b;
a ^= b; b ^= a; a ^= b;
  • The XOR operator has these important properties: a ^ a = 0, a ^ 0 = a, and a ^ b ^ a = b. Because XOR is its own inverse, it can swap values efficiently.
  • Step 1: a ^= b → a = 5 ^ 3 = 6. Now a = 6, b = 3.
  • Step 2: b ^= a → b = 3 ^ 6 = 5. Now a = 6, b = 5.
  • Step 3: a ^= b → a = 6 ^ 5 = 3. Final values: a = 3, b = 5 — successfully swapped.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.