Multiple choice technology programming languages

What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer. public static ShortCkt { public static void main(String args[]) { int i = 0; boolean t = true; boolean f = false, b; b = (t && ((i++) == 0)); b = (f && ((i+=2) > 0)); System.out.println(i); } }

  1. 0

  2. 1

  3. 2

  4. 3

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Due to short-circuit evaluation, the first expression (t && ((i++) == 0)) executes i++ making i=1. The second expression (f && ((i+=2) > 0)) short-circuits because f is false, so i+=2 never executes. Final i=1.

AI explanation

Trace the code: i=0. First: b = (t && ((i++)0)) — t is true, so the right side evaluates: i++ returns 0 (post-increment, so the comparison uses the old value 00 → true) and i becomes 1. Second: b = (f && ((i+=2)>0)) — f is false, and && short-circuits, so the right side ((i+=2)>0) is never evaluated at all; i stays at 1. The final println(i) therefore prints 1. This is a classic short-circuit evaluation test, and '1' is correct.