← Quiz: Wrapper Classes and Autoboxing: all 10 questions
Quiz: Wrapper Classes and Autoboxing · Question 4 of 10
Which line can throw a NullPointerException even though it calls no method?
Which line can throw a NullPointerException even though it calls no method?
- A.Integer x = null;
- B.int x = map.get("key"); where map is a Map<String, Integer>
- C.Integer x = map.get("key");
- D.String s = null;
Answer & explanation
Correct answer: int x = map.get("key"); where map is a Map<String, Integer>
If the key is missing, get returns null and unboxing it to int calls intValue() on null.
Can you get the rest right without peeking?
Take the timed Quiz: Wrapper Classes and Autoboxing quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →