Java OCA OCP Practice Question 585

Question

What is the output of the following code?


Integer i = null; 
if (i != null  &  i.intValue() == 5) 
     System.out.println("Value is 5"); 
  • A. Prints "Value is 5".
  • B. Throws an exception.


B.

Note

The & operator does not short circuit.

Even though the left-hand operand is null, the right-hand operand is still evaluated.

Attempting to call the intValue() method on null results in a NullPointerException.




PreviousNext

Related