← Quiz: HashMap and Maps: all 10 questions
Quiz: HashMap and Maps · Question 10 of 10
What does this print? (code)
What does this print?
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String, Integer> m = new HashMap<>();
m.put(null, 1);
m.put(null, 2);
System.out.println(m.get(null) + " " + m.size());
}
}
- A.1 2
- B.NullPointerException
- C.2 2
- D.2 1
Answer & explanation
Correct answer: 2 1
HashMap allows one null key; the second put replaces its value. Hashtable and ConcurrentHashMap do not allow null keys.
Can you get the rest right without peeking?
Take the timed Quiz: HashMap and Maps quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →