Java HashMap MCQ - Multiple Choice Questions and Answers

1. What is HashMap in Java?

a) A list-based collection
b) A set-based collection
c) A key-value pair collection
d) A stack-based collection

Answer:

c) A key-value pair collection

Explanation:

HashMap is a collection in Java that stores data in key-value pairs, allowing for fast retrieval.

2. What interface does HashMap implement?

a) List
b) Set
c) Map
d) Queue

Answer:

c) Map

Explanation:

HashMap implements the Map interface in Java.

3. Can a HashMap contain duplicate keys?

a) Yes
b) No
c) Only null keys
d) Only integer keys

Answer:

b) No

Explanation:

In a HashMap, each key is unique. Adding a new entry with an existing key replaces the old value with the new one.

4. Is HashMap ordered?

a) Yes, insertion order
b) Yes, natural ordering
c) No, it's unordered
d) Yes, alphabetical order

Answer:

c) No, it's unordered

Explanation:

HashMap does not maintain any order of its entries, neither insertion order nor natural ordering.

5. Can a HashMap contain null values?

a) Yes
b) No
c) Only one null value
d) Only null keys, not values

Answer:

a) Yes

Explanation:

HashMap allows null values and even one null key.

6. What is the default initial capacity of a HashMap in Java?

a) 8
b) 10
c) 12
d) 16

Answer:

d) 16

Explanation:

The default initial capacity of a HashMap is 16.

7. How does HashMap handle collisions?

a) By resizing the map
b) By using a linked list or tree at each entry
c) By skipping the addition of new elements
d) By removing the existing element

Answer:

b) By using a linked list or tree at each entry

Explanation:

HashMap handles collisions by storing collided elements in a linked list or tree structure at the same index.

8. What method do you use to get a value from a HashMap?

a) getValue()
b) findValue()
c) get()
d) lookup()

Answer:

c) get()

Explanation:

The get() method is used to retrieve a value from a HashMap using its key.

9. How do you remove a key-value pair from a HashMap?

a) remove()
b) delete()
c) erase()
d) discard()

Answer:

a) remove()

Explanation:

The remove() method is used to remove a key-value pair from a HashMap.

10. What happens if you put a key-value pair with an existing key in a HashMap?

a) The map remains unchanged
b) A duplicate key is created
c) The old value is replaced by the new value
d) An exception is thrown

Answer:

c) The old value is replaced by the new value

Explanation:

If a key already exists in the map, putting a new value for that key will replace the old value.

11. What is the time complexity of the get() and put() methods in HashMap?

a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)

Answer:

a) O(1)

Explanation:

In most cases, the get() and put() methods in HashMap have a time complexity of O(1).

12. How can you iterate over a HashMap?

a) Using a for loop
b) Using an iterator
c) Both a and b
d) HashMaps cannot be iterated

Answer:

c) Both a and b

Explanation:

A HashMap can be iterated using an iterator or a for-each loop over the entrySet, keySet, or values.

13. What method checks if a HashMap contains a specific key?

a) containsKey()
b) hasKey()
c) findKey()
d) keyExists()

Answer:

a) containsKey()

Explanation:

The containsKey() method is used to check if a particular key exists in the HashMap.

14. What does the clear() method do in a HashMap?

a) Clears the values but keeps the keys
b) Removes all key-value mappings
c) Resets the capacity of the map
d) Removes only null keys and values

Answer:

b) Removes all key-value mappings

Explanation:

The clear() method removes all of the mappings from the map.

15. Can a HashMap be synchronized?

a) Yes
b) No
c) Only in multi-threaded applications
d) Only if explicitly defined

Answer:

a) Yes

Explanation:

While HashMap itself is not synchronized, it can be wrapped with Collections.synchronizedMap() to make it synchronized.


Comments