Java LinkedHashMap MCQ Questions and Answers

1. What is a LinkedHashMap in Java?

a) A type of array
b) A type of linked list
c) A type of set
d) A hash table and linked list implementation of the Map interface

Answer:

d) A hash table and linked list implementation of the Map interface

Explanation:

LinkedHashMap is a Map implementation that maintains a linked list of the entries in the map, in the order in which they were inserted.

2. How does LinkedHashMap maintain the order of elements?

a) Using a hash table
b) By sorting the keys
c) Through a doubly-linked list
d) Using an array

Answer:

c) Through a doubly-linked list

Explanation:

LinkedHashMap maintains the insertion order of its entries through a doubly-linked list.

3. Can a LinkedHashMap contain duplicate keys?

a) Yes
b) No
c) Only integer keys can be duplicated
d) Only string keys can be duplicated

Answer:

b) No

Explanation:

Like any other Map implementation, LinkedHashMap cannot have duplicate keys.

4. What is the default initial capacity of a LinkedHashMap?

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

Answer:

d) 16

Explanation:

The default initial capacity of a LinkedHashMap is 16.

5. Which method is used to access elements in a LinkedHashMap?

a) access()
b) get()
c) retrieve()
d) find()

Answer:

b) get()

Explanation:

The get() method is used to access elements in a LinkedHashMap by their key.

6. Can a LinkedHashMap contain null values and null key?

a) Yes
b) No
c) Only null values, not null key
d) Only null key, not null values

Answer:

a) Yes

Explanation:

LinkedHashMap allows one null key and multiple null values.

7. What is the time complexity of basic operations like put and get in a LinkedHashMap?

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

Answer:

a) O(1)

Explanation:

Basic operations like put and get have a constant time complexity in a LinkedHashMap.

8. How does a LinkedHashMap differ from a HashMap in Java?

a) LinkedHashMap is synchronized
b) LinkedHashMap maintains insertion order
c) LinkedHashMap allows duplicate keys
d) LinkedHashMap uses less memory

Answer:

b) LinkedHashMap maintains insertion order

Explanation:

The main difference between LinkedHashMap and HashMap is that LinkedHashMap maintains the insertion order of entries.

9. How do you remove all entries from a LinkedHashMap?

a) clear()
b) removeAll()
c) empty()
d) deleteAll()

Answer:

a) clear()

Explanation:

The clear() method is used to remove all the mappings from a LinkedHashMap.

10. Is the iteration order over a LinkedHashMap predictable?

a) Yes, it follows insertion order
b) No, it's random
c) Yes, it follows natural ordering
d) No, it follows hash code ordering

Answer:

a) Yes, it follows insertion order

Explanation:

The iteration over a LinkedHashMap is predictable as it follows the insertion order of the entries.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top