Search Tutorials


Java LinkedHashMap MCQ Questions and Answers | JavaInUse

Java LinkedHashMap MCQ Questions and Answers

Q. What is the order of elements in a LinkedHashMap in Java?

A. Insertion order
B. Reverse insertion order
C. Alphabetical order
D. Reverse alphabetical order

Q. Which interface does LinkedHashMap implement in Java?

A. Map
B. Set
C. List
D. Queue

Q. How does LinkedHashMap maintain insertion order in Java?

A. By using a doubly linked list
B. By using a singly linked list
C. By using a red-black tree
D. By using a hash table

Q. What is the time complexity of get() operation in LinkedHashMap in Java?

A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)

Q. What is the time complexity of put() operation in LinkedHashMap in Java?

A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)

Q. What is the time complexity of removing an element from a LinkedHashMap in Java?

A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)

Q. How can you iterate over the keys in a LinkedHashMap in Java?

A. Using a for-each loop
B. Using an iterator
C. Using a while loop
D. All of the above

Q. What is the output order of keys() iterator in LinkedHashMap in Java?

A. Insertion order
B. Reverse insertion order
C. Alphabetical order
D. Reverse alphabetical order

Q. How can you create a LinkedHashMap with a specific initial capacity in Java?

A. LinkedHashMap(int initialCapacity)
B. LinkedHashMap(Map m)
C. LinkedHashMap(int initialCapacity, float loadFactor)
D. All of the above

Q. What is the load factor of a LinkedHashMap by default in Java?

A. 0.5
B. 0.75
C. 1.0
D. 0.25

Q. What is the output order of values() iterator in LinkedHashMap in Java?

A. Insertion order
B. Reverse insertion order
C. Alphabetical order of keys
D. Reverse alphabetical order of keys





Q. How can you access the first entry in a LinkedHashMap in Java?

A. Using map.get(0)
B. Using map.getFirstEntry()
C. Using map.firstKey()
D. Using map.entrySet().iterator().next()

Q. What is the purpose of the accessOrder parameter in LinkedHashMap constructor in Java?

A. To specify the order of iteration
B. To control the load factor
C. To specify the initial capacity
D. To control the hashing algorithm

Q. How can you convert a HashMap to a LinkedHashMap in Java?

A. By creating a new LinkedHashMap and copying entries
B. By using the asLinkedHashMap() method
C. By using the toLinkedHashMap() method
D. By casting HashMap to LinkedHashMap

Q. What is the output order of entrySet() iterator in LinkedHashMap in Java?

A. Insertion order
B. Reverse insertion order
C. Alphabetical order of keys
D. Reverse alphabetical order of keys

Q. What is the output of the following code?

LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
A.
one 1
two 2
three 3
B.
three 3
two 2
one 1
C.
one 3
two 2
three 1
D.
An error occurs

Q. What will be the output of the following code?

LinkedHashMap<String, Integer> map = new LinkedHashMap<>(2, 0.75f, true);
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
System.out.println(map.get("two"));
A.
2
B.
null
C.
An error occurs
D.
3

Q. What will be the output of the following code?

LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("one", 10);
System.out.println(map.get("one"));
A.
1
B.
10
C.
null
D.
An error occurs

Q. What will be the output of the following code?

LinkedHashMap<String, Integer> map1 = new LinkedHashMap<>();
map1.put("one", 1);
map1.put("two", 2);
LinkedHashMap<String, Integer> map2 = new LinkedHashMap<>(map1);
map2.put("three", 3);


System.out.println(map1);
System.out.println(map2);
A.
{one=1, two=2}
{one=1, two=2, three=3}
B.
{one=1, two=2}
{one=1, two=2}
C.
{one=1, two=2, three=3}
{one=1, two=2, three=3}
D.
An error occurs

Q. What will be the output of the following code?

LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.remove("two");


System.out.println(map.get("two"));
A.
2
B.
null
C.
An error occurs
D.
3

Q. What will be the output of the following code?

LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.keySet().remove("two");


System.out.println(map);
A.
{one=1, three=3}
B.
{one=1, two=2, three=3}
C.
An error occurs
D.
{}

Q. What will be the output of the following code?

LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.entrySet().remove(new AbstractMap.SimpleEntry<>("two", 2));


System.out.println(map);
A.
{one=1, three=3}
B.
{one=1, two=2, three=3}
C.
An error occurs
D.
{}

Q. What will be the output of the following code?

LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.keySet().clear();


System.out.println(map);
A.
{}
B.
{one=1, two=2, three=3}
C.
An error occurs
D.
null

Q. What will be the output of the following code?

LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.entrySet().clear();


System.out.println(map);
A.
{}
B.
{one=1, two=2, three=3}
C.
An error occurs
D.
null

Q. What will be the output of the following code?

LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.keySet().addAll(Map.of("four", 4, "five", 5));


System.out.println(map);
A.
{one=1, two=2, three=3, four=4, five=5}
B.
{one=1, two=2, three=3}
C.
An error occurs
D.
{four=4, five=5}