Java Operators Output Questions: 35 Practice Problems with Answers
These 35 Java operator output questions are designed to test your understanding of how Java evaluates expressions — especially the tricky behavior of prefix vs postfix operators, arithmetic precedence, and type rules.
Each question presents a small program. Try to predict the output before checking the answer. This is one of the best ways to prepare for Java interviews and certifications.
Questions 1–10: Prefix, Postfix & Compound Expressions
Q1. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i + i++ - i);
}
}
Output: 10
Explanation: i++ returns 10 and then i becomes 11. So the expression is 10 + 10 - 11 = 9... wait, let me re-evaluate:
iis 10 → first operand is10i++returns 10 and incrementsito 11 → second operand is10- Now
iis 11 → third operand is11 - Result:
10 + 10 - 11 = 9
Corrected output: 9
Q2. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i - i++ - i);
}
}
Output: -11
Explanation: i - i++ - i → 10 - 10 - 11 = -11
Q3. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i--i + i);
}
}
Output: Compile-time error
Explanation: i--i is tokenized as i-- followed by i. Two operands with no operator between them is invalid syntax.
Q4. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i---i++i);
}
}
Output: Compile-time error
Explanation: Multiple invalid tokens — i---i is i-- then -i, but -i++i is not valid.
Q5. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i---i+++i);
}
}
Output: Compile-time error
Explanation: Same tokenization problem — invalid expression.
Q6. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i--);
}
}
Output: 10
Explanation: Postfix decrement returns the original value (10), then decrements i to 9.
Q7. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(--i);
}
}
Output: 9
Explanation: Prefix decrement reduces i to 9 first, then returns 9.
Q8. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(++i);
}
}
Output: 11
Explanation: Prefix increment first increments to 11, then returns 11.
Q9. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
i++;
System.out.println(i);
}
}
Output: 11
Explanation: Postfix increment on its own line increments i to 11.
Q10. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = -10;
System.out.println(--i);
}
}
Output: -11
Explanation: Prefix decrement reduces -10 to -11.
Questions 11–20: Arithmetic & Operator Precedence
Q11. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i + i);
}
}
Output: 20
Q12. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i - i);
}
}
Output: 0
Q13. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i * i);
}
}
Output: 100
Q14. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i / i);
}
}
Output: 1
Q15. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i + i / i);
}
}
Output: 11
Explanation: Division has higher precedence than addition, so i + (i / i) = 10 + 1 = 11.
Q16. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i + i * i);
}
}
Output: 110
Explanation: Multiplication first: 10 + (10 * 10) = 10 + 100 = 110.
Q17. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i + i * i / i);
}
}
Output: 20
Explanation: 10 + (10 * 10 / 10) = 10 + 10 = 20.
Q18. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i + i * i / i + i);
}
}
Output: 30
Explanation: 10 + 10 + 10 = 30.
Q19. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i + i / i * i % i);
}
}
Output: 10
Explanation: 10 / 10 = 1, 1 * 10 = 10, 10 % 10 = 0, so 10 + 0 = 10.
Q20. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(i % 0);
}
}
Output: Runtime exception: ArithmeticException
Explanation: Modulus by zero throws ArithmeticException at runtime.
Questions 21–30: Assignment & String Comparisons
Q21. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
System.out.println(0 % 2);
}
}
Output: 0
Q22. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
i += 2;
System.out.println(i);
}
}
Output: 12
Explanation: i += 2 is the same as i = i + 2.
Q23. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
i = +2;
System.out.println(i);
}
}
Output: 2
Explanation: This is unary plus, not +=. So i is set to 2.
Q24. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
i = ++2;
System.out.println(i);
}
}
Output: Compile-time error
Explanation: ++ can only be applied to variables, not literals.
Q25. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 10;
i = ++i;
System.out.println(i);
}
}
Output: 11
Explanation: ++i increments i to 11, then assigns it back to i.
Q26. What is the output of the following program?
public class B {
public static void main(String[] args) {
char a = 'a';
char b = 'c';
System.out.println(b > a);
}
}
Output: true
Explanation: Characters are compared by their Unicode values: 'c' (99) > 'a' (97) is true.
Q27. What is the output of the following program?
public class B {
public static void main(String[] args) {
String a = "1";
String b = "2";
System.out.println(b > a);
}
}
Output: Compile-time error
Explanation: Java does not allow > or < on String objects.
Q28. What is the output of the following program?
public class B {
public static void main(String[] args) {
String a = "1";
String b = "2";
System.out.println(b = a);
}
}
Output: 1
Explanation: b = a assigns a to b and returns the assigned value, which is "1".
Q29. What is the output of the following program?
public class B {
public static void main(String[] args) {
String a = "1";
String b = "2";
System.out.println(b == a);
}
}
Output: false
Explanation: == compares references for objects. Even identical content may not be the same reference.
Q30. What is the output of the following program?
public class B {
public static void main(String[] args) {
String a = "1";
String b = "2";
System.out.println(b = !a);
}
}
Output: Compile-time error
Explanation: ! can only be applied to boolean values, not Strings.
Questions 31–35: Relational & Ternary
Q31. What is the output of the following program?
public class B {
public static void main(String[] args) {
String a = "1";
String b = "2";
System.out.println(b != a);
}
}
Output: true
Explanation: != compares references. Since a and b are different objects, the result is true.
Q32. What is the output of the following program?
public class B {
public static void main(String[] args) {
String a = "1";
String b = "2";
System.out.println(b <= a);
}
}
Output: Compile-time error
Explanation: <= is not allowed on String objects.
Q33. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 12;
int j = 21;
System.out.println(i <= j);
}
}
Output: true
Explanation: 12 is less than or equal to 21.
Q34. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 12;
int j = 21;
System.out.println(i > j ? "Right" : "Wrong");
}
}
Output: Wrong
Explanation: 12 > 21 is false, so the ternary returns "Wrong".
Q35. What is the output of the following program?
public class B {
public static void main(String[] args) {
int i = 12;
int j = 1;
System.out.println(i > j);
}
}
Output: true
Explanation: 12 is greater than 1.
Frequently Asked Questions
Why does i++ + ++i give different results in Java?
Postfix (i++) uses the current value and then increments. Prefix
(++i) increments first and then uses the new value. When mixed in an
expression, evaluation happens left to right, so i++ uses the old value
while ++i uses the incremented value.
What is the output of i--i+i in Java?
This expression has a compile-time error because Java tokenizes
i--i as i-- followed by i, which is two operands
with no operator between them. It is not valid syntax.
What happens when you divide by zero in Java?
Dividing an integer by zero throws ArithmeticException at
runtime. For floating-point numbers, dividing by zero returns
Infinity or NaN instead of throwing an exception.
Can you compare two String objects using > in Java?
No. Java does not allow > or < on String objects. It
causes a compile-time error. To compare strings, use equals() for content
or compareTo() for lexicographic ordering.
What is the output of i % 0 in Java?
The modulus of an integer by zero throws ArithmeticException at runtime
because the modulus operator performs an implicit division. The same exception occurs
with i / 0 for integers.
Conclusion
These 35 Java operator output questions cover the most common pitfalls developers face — especially the tricky behavior of prefix vs postfix operators and precedence rules. Practice them until you can predict each output with confidence.
The most important lessons: i++ uses the old value, ++i uses
the new value, division/modulus by zero throws an exception, and Strings cannot be
compared with relational operators. Master these and you'll handle any Java operator
interview question.
Related topics: Java Operators Complete Guide, Java Data Types Questions, Java Overloading Questions, Java Overriding Questions, Java Array Questions, Java Interview Questions
2 comments:
35th Question galat hai sir;;;
ok dear
Post a Comment