Multiple choice

What will be the output of the program (in jdk1.6 or above)? public class BoolTest { public static void main(String [] args) { Boolean b1 = new Boolean("false"); boolean b2; b2 = b1.booleanValue(); if (!b2) { b2 = true; System.out.print("x "); } if (b1 & b2) /* Line 13 */ { System.out.print("y "); } System.out.println("z"); } }

  1. z

  2. x z

  3. y z

  4. Compilation fails.

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

b1 is a Boolean object with value false. b2 gets b1's primitive value (false). The !b2 condition is true (NOT false = true), so b2 becomes true and "x" prints. Line 13: b1 & b2 uses Boolean objects. In JDK 1.6+, Boolean objects can use & operator with unboxing. b1 unboxes to false, b2 is true. false & true = false, so the if block doesn't execute. Finally "z" prints. Output: "x z".