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?
What is the output of:
int x = 10 % 3;
- The modulus/remainder operator % returns the remainder after division.
- 10 ÷ 3 = 3 remainder 1, so 10 % 3 = 1.
- == 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.
- 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.
- 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.
- ^= 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);What is the output of the following code snippet?
int a = 5;
System.out.println(a++ + ++a);- 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.
- >>> 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 >>.
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);- 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);Predict the output of the following program.
int a = 5;
int b = 10;
int c = (a > b) ? a : b;
System.out.println(c);- 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);- ~ 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);- 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);- 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);- 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");- 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- 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.
- -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);- 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.
- 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)).
- 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.
- 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);- 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.
- & 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);- 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);- 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);- 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.
- 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.




