Java OCA OCP Practice Question 268

Question

How many of the following lines compile?

int i = null; 
Integer in = null; 
String s = null; 
  • A. None
  • B. One
  • C. Two
  • D. Three


C.

Note

Objects are allowed to have a null reference while primitives cannot.

int is a primitive, so assigning null to it does not compile.

Integer and String are both objects and can therefore be assigned a null reference.

Option C is correct.




PreviousNext

Related