Java HashMap
Test your knowledge of HashMap: hashing, collisions, key-value operations, null handling, iteration, and performance.
Questions in this quiz
20 questions on Java HashMap. Work through them here, then start the quiz to answer them and see your score.
1.What data structure does HashMap use internally?
- AA stack
- BAn array of linked lists (buckets)
- CA sorted binary tree
- DA doubly linked list
2.What is the time complexity of get() and put() in a HashMap under normal conditions?
- AO(1) average
- BO(n²)
- CO(n)
- DO(log n)
3.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { Map<String, Integer> scores = new HashMap<>(); scores.put("Alice", 90); scores.put("Bob", 85); scores.put("Alice", 95); System.out.println(scores.get("Alice")); System.out.println(scores.size()); } }- A95 3
- B90 2
- C95 2
- D90 3
4.Does HashMap maintain insertion order?
- AYes, always in insertion order
- BYes, in reverse insertion order
- CNo, order is not guaranteed
- DYes, but only for String keys
5.What happens when you call get() with a key that does not exist in a HashMap?
- AThrows NullPointerException
- BReturns 0 for numeric value types
- CReturns null
- DThrows NoSuchElementException
6.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("a", 1); map.put("b", 2); map.put("c", 3); System.out.println(map.getOrDefault("b", 0)); System.out.println(map.getOrDefault("z", 99)); } }- A0 99
- B2 0
- C2 99
- D0 0
7.Can a HashMap have a null key?
- ANo, null keys throw NullPointerException
- BOnly if the value is also null
- CYes, exactly one null key is allowed
- DYes, multiple null keys are allowed
8.What does computeIfAbsent() do?
- AComputes and inserts a value only if the key is absent or mapped to null
- BThrows an exception if the key is already present
- CComputes a new value regardless of whether the key exists
- DRemoves the key if the value is null
9.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { Map<String, List<String>> groups = new HashMap<>(); String[] words = {"apple", "ant", "banana", "avocado", "blueberry"}; for (String w : words) { groups.computeIfAbsent(String.valueOf(w.charAt(0)), k -> new ArrayList<>()).add(w); } System.out.println(groups.get("a").size()); } }- A5
- B1
- C3
- D2
10.What is a hash collision?
- AWhen get() returns null unexpectedly
- BWhen a HashMap runs out of memory
- CWhen two different keys produce the same hash bucket index
- DWhen two keys are equal
11.Why must you override hashCode() when you override equals() in a key class?
- ABecause the HashMap contract requires equal objects to have equal hash codes
- BBecause Java will throw an error otherwise
- CTo allow null keys in the map
- DFor performance reasons only
12.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("x", 10); map.put("y", 20); map.put("z", 30); map.merge("x", 5, Integer::sum); map.merge("w", 5, Integer::sum); System.out.println(map.get("x")); System.out.println(map.get("w")); } }- A10 null
- B15 5
- C10 5
- D5 5
13.What is the default load factor of a HashMap?
- A1.0
- B0.5
- C2.0
- D0.75
14.How do you iterate over all key-value pairs in a HashMap?
Map<String, Integer> map = new HashMap<>(); map.put("a", 1); map.put("b", 2); for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + "=" + entry.getValue()); }- ACompilation error — cannot use entrySet()
- BThrows ConcurrentModificationException immediately
- CPrints only keys
- DPrints each key=value pair (order not guaranteed)
15.What is the difference between HashMap and Hashtable?
- AHashtable is faster; HashMap is slower
- BThere is no meaningful difference in modern Java
- CHashMap is sorted; Hashtable is unsorted
- DHashMap allows null keys/values and is not synchronised; Hashtable disallows nulls and is synchronised
16.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("a", 1); map.put("b", 2); map.put("c", 3); map.entrySet().removeIf(e -> e.getValue() > 1); System.out.println(map); } }- A{a=1}
- B{a=1, b=2, c=3}
- C{}
- D{b=2, c=3}
17.What is LinkedHashMap and when would you use it?
- AA HashMap sorted by keys
- BA HashMap backed by a linked list instead of an array
- CA thread-safe version of HashMap
- DA HashMap that also maintains a doubly linked list to preserve insertion order
18.What does putIfAbsent() do differently from put()?
- AputIfAbsent() always overwrites the existing value
- BputIfAbsent() only inserts if the key is not already present — it never overwrites
- CputIfAbsent() works only with null values
- DputIfAbsent() throws an exception if the key exists
19.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { Map<String, Integer> wordCount = new HashMap<>(); String[] words = {"cat","dog","cat","bird","dog","cat"}; for (String w : words) { wordCount.merge(w, 1, Integer::sum); } System.out.println(wordCount.get("cat")); System.out.println(wordCount.get("dog")); } }- A1 1
- B3 2
- C3 1
- D2 3
20.What is ConcurrentHashMap and when should you use it?
- AA thread-safe HashMap replacement that allows concurrent reads and segment-level locking for writes
- BA HashMap backed by a database
- CA HashMap that throws exceptions on concurrent modification
- DA HashMap that sorts keys concurrently