Java OCA OCP Practice Question 3232

Question

Consider the following code segment:

Boolean b = null;
System.out.println(b ? true : false);

Which one of the following options correctly describes the behavior of this code segment?

  • a) This code will result in a compiler error since a reference type (of type Boolean) cannot be used as part of expression for condition check.
  • b) This code will result in a throwing a NullPointerException.
  • c) This code will print true in console.
  • d) This code will print false in console.


b)

Note

Note that unboxing can take place in expressions when you use a wrapper type object in place of a primitive type value.

In this case, in the condition check for the conditional operator (?: operator), a primitive boolean value is required, but a wrapper type object is provided.

Hence auto-unboxing occurs, with the reference pointing to null.

As a result, this code segment results in throwing a NullPointerException.




PreviousNext

Related