Search Tutorials


Java LinkedHashSet MCQ Questions and Answers | JavaInUse

Java LinkedHashSet MCQ Questions and Answers

Q. What is the order of elements in a LinkedHashSet?

A. Insertion order
B. Reverse insertion order
C. Sorted by default
D. Unspecified order

Q. Which interface does LinkedHashSet implement?

A. Set
B. 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 loop
B. 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() method
B. Using a for loop and an array
C. Using the values() method
D. Using the clone() method





Q. What is the output of the following code?

LinkedHashSet<String> colors = new LinkedHashSet<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.add("Yellow");
colors.add("Red");
for (String color : colors) {
System.out.print(color + " ");
}
A.
Red Green Blue Yellow
B.
Red Green Blue Yellow Red
C.
Green Blue Yellow Red
D.
Null output

Q. Which of the following statements is true about LinkedHashSet?

A. LinkedHashSet maintains the insertion order of elements and allows duplicates.
B. LinkedHashSet maintains the insertion order of elements but does not allow duplicates.
C. LinkedHashSet does not maintain the insertion order of elements and allows duplicates.
D. LinkedHashSet is an unordered collection and does not allow null elements.

Q. How can you create an empty LinkedHashSet of Integer type?

A.
LinkedHashSet<Integer> set = new LinkedHashSet<>();
B.
LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
C.
LinkedHashSet<Integer> set = new LinkedHashSet<>(<Integer>);
D.
LinkedHashSet<Integer> set = new LinkedHashSet();

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

LinkedHashSet<String> fruits = new LinkedHashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.remove("Banana");
for (String fruit : fruits) {
System.out.print(fruit + " ");
}
A.
Apple Orange
B.
Apple Banana Orange
C.
Banana Orange
D.
Apple

Q. How can you create a LinkedHashSet with initial capacity of 10?

A.
LinkedHashSet<String> set = new LinkedHashSet<>(10);
B.
LinkedHashSet<String> set = new LinkedHashSet<>();
set.ensureCapacity(10);
C.
LinkedHashSet<String> set = new LinkedHashSet<String>(10);
D.
LinkedHashSet<String> set = new LinkedHashSet<>();
set.setSize(10);

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

LinkedHashSet<Integer> numbers = new LinkedHashSet<>();
numbers.add(3);
numbers.add(1);
numbers.add(5);
numbers.add(4);
numbers.add(2);
for (int num : numbers) {
System.out.print(num + " ");
}
A.
3 1 5 4 2
B.
3 5 1 4 2
C.
3 1 4 5 2
D.
1 2 3 4 5

Q. Which of the following methods can be used to iterate through a LinkedHashSet?

A.
for (Object obj : linkedHashSet) {
    // process obj
}
B.
Iterator<Object> iter = linkedHashSet.iterator();
while (iter.hasNext()) {
    Object obj = iter.next();
    // process obj
}
C. Both A and B are correct.
D. Neither A nor B are correct.

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

LinkedHashSet<String> animals = new LinkedHashSet<>();
animals.add("Cat");
animals.add("Dog");
animals.add("Bird");
animals.addAll(List.of("Rabbit", "Fish"));
for (String animal : animals) {
System.out.print(animal + " ");
}
A.
Cat Dog Bird Rabbit Fish
B.
Cat Dog Bird Fish Rabbit
C.
Cat Dog Rabbit Fish Bird
D.
Cat Dog Bird

Q. How can you check if a LinkedHashSet is empty?

A.
if (linkedHashSet.isEmpty()) {
    // set is empty
}
B.
if (linkedHashSet.size() == 0) {
    // set is empty
}
C. Both A and B are correct.
D.
Neither A nor B are correct.

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

LinkedHashSet<String> shapes = new LinkedHashSet<>();
shapes.add("Circle");
shapes.add("Square");
shapes.add("Triangle");
shapes.remove("Square");
shapes.add("Rectangle");
for (String shape : shapes) {
System.out.print(shape + " ");
}
A.
Circle Square Triangle Rectangle
B.
Circle Triangle Square Rectangle
C.
Circle Triangle Rectangle
D.
Circle Rectangle Triangle