Font size
WorksheetsJava HashMap Quiz
Total questions: 14
Worksheet time: 7mins
What is a HashMap in Java used for?
To store data in key-value pairs.
To perform mathematical calculations.
To manage threads.
To handle exceptions.
Which of the following is true about a HashMap?
It maintains the order of insertion.
It does not allow null keys.
It is synchronized.
It allows null values.
What is the primary difference between HashMap and Hashtable in Java?
Hashtable is synchronized, while HashMap is not.
HashMap allows null keys and values, while Hashtable does not.
Both A and B are correct.
There is no difference.
Which method would you use to check if a HashMap contains a specific key?
get()
containsKey()
containsValue()
size()
How can you remove a key-value pair from a HashMap?
(a)
What will happen if you try to store a duplicate key in a HashMap?
It will replace the old value with the new one.
It will throw an exception.
It will ignore the new value.
It will store both values.
Which of the following is NOT a method in the HashMap class?
put()
get()
insert()
remove()
What does the `put()` method in a HashMap return?
The previous value associated with the key, or null if there was no mapping for the key.
True if insertion is successful.
The current value associated with the key.
None of the above.
Which collection view can you obtain from a HashMap?
Keys only
Values only
Key-value pairs
All of the above
What is the default initial capacity of a HashMap in Java?
8
12
16
20
Can a HashMap have multiple null values?
Yes
No
Only if there are no null keys
Only if there is one null key
What interface does HashMap implement?
List
Set
Map
Collection
Which of the following is a characteristic of a HashMap?
Ordered
Unordered
Immutable
Concurrent
How does a HashMap handle collisions?
By using a linked list to store multiple entries at the same index.
By resizing the map.
By storing the new entry in the next available space.
By overwriting the existing entry.
