ExamHoot

What is the output of the following code?

int a = 2, b = 3;
boolean result = a++ > 2 || b++ > 3 && a == 3;
System.out.println(result + "," + a + "," + b);
  1. true,3,4

  2. false,3,4

  3. true,2,3

  4. false,2,4

Show answer & explanation

Correct answer

false,3,4

Explanation

`a++ > 2` is false (2>2 false) then a becomes 3. Short-circuit OR: left is false, so right side is evaluated. Right side: `b++ > 3` is false (3>3 false) then b=4, and `&&` short-circuits because left is false, so `a == 3` is not evaluated. So `false || false` = false. Output false,3,4.

Written by ExamHoot EditorialPublished · Updated

All 10 Java -operators questions

  1. 1.Given the expression `int a = 5; int b = 10; int c = ++a * b-- + a;`, what is the value of `c` after evaluation?
  2. 2.What is the result of evaluating `(2 + 3) * 4 == 20 || !(5 > 3) && (6 / 2 == 3)` in Java?
  3. 3.Given `int x = 7; int y = 3; int z = x ^ y;` and then `z = z << 1;`, what is the final value of `z`?
  4. 4.Which of the following expressions evaluates to `true` for all non-negative integers `n`?
  5. 5.Consider `short s = 10; s = s + 5;` and `short t = 10; t += 5;`.
  6. 6.What is the output of the following code?
  7. 7.Given `int x = 5; x = x++ + ++x;`, what is the final value of `x`?
  8. 8.What is the value of `(int) (3.9 + 5.1 / 2.0)` in Java?
  9. 9.Given `boolean a = true; boolean b = false; boolean c = a | b & !a;`, what is the value of `c`?
  10. 10.Which of these expressions is guaranteed to produce arithmetic overflow?