Skip to content
Sharp

Core Java · interview question

What does this LinkedHashSet print after inserting a duplicate fruit?

Multiple choiceMediumchapter-4-curatedpremiumcore-javacode-reading-with-collections
java
Set<String> fruits = new LinkedHashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Apple");
System.out.println(fruits);
Choose one answer
Answer and explanation

Correct answer

B. [Apple, Banana, Cherry]

Why

HashSet does not allow duplicates and does not maintain any order, but LinkedHashSet does maintain insertion order. The list retains order without duplicates.

Test yourself on Core Java

A free test with instant, explained results. No account needed.

Start free test
All Core Java questions →