Java LinkedHashSet MCQ Questions and Answers
Q. What is the order of elements in a LinkedHashSet?
A. Insertion orderB. Reverse insertion order
C. Sorted by default
D. Unspecified order
Q. Which interface does LinkedHashSet implement?
A. SetB. List
C. Queue
D. Deque
Q. How is LinkedHashSet different from HashSet?
A. LinkedHashSet maintains insertion order, HashSet does not.B. LinkedHashSet allows duplicates, HashSet does not.
C. LinkedHashSet is slower than HashSet.
D. LinkedHashSet is an ordered version of TreeSet.
Q. What is the output of the following code snippet?
LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Orange");
set.add("Apple");
for (String fruit : set) {
System.out.print(fruit + " ");
}
A. Apple Banana Orange
B. Banana Orange Apple
C. Orange Banana Apple
D. Apple Apple Banana Orange
Q. Which method is used to create a shallow copy of a LinkedHashSet?
A. clone()B. toArray()
C. copy()
D. addAll()
Q. What is the time complexity of the add() operation in LinkedHashSet?
A. O(1)B. O(log n)
C. O(n)
D. O(n log n)
Q. How do you iterate over the elements in a LinkedHashSet?
A. Using a for-each loopB. Using an iterator
C. Using a while loop and the get() method
D. All of the above
Q. What is the output of the following code snippet?
LinkedHashSet<Integer> numbers = new LinkedHashSet<>();
numbers.add(3);
numbers.add(1);
numbers.add(2);
System.out.println(numbers.contains(2));
numbers.remove(1);
System.out.println(numbers.contains(1));
A. true
false
B. false
true
C. true
true
D. false
false
Q. What is the result of the following code snippet?
LinkedHashSet<String> colors = new LinkedHashSet<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.remove("Green");
colors.add("Yellow");
System.out.println(colors);
A. [Red, Green, Blue, Yellow]
B. [Red, Blue, Yellow]
C. [Red, Blue]
D. [Green, Blue, Yellow]
Q. How do you convert a LinkedHashSet to an array?
A. Using the toArray() methodB. Using a for loop and an array
C. Using the values() method
D. Using the clone() method