ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
Set<String> s = new LinkedHashSet<>(List.of("a", "b", "c"));
s.remove("a");
s.add("a");
s.add("b");
System.out.print(s);
  1. A. [a, b, c]
  2. B. [b, c, a, b]
  3. C. [c, a, b]
  4. D. [b, c, a]

Correct answer: D. [b, c, a]

LinkedHashSet keeps insertion order. After removal, "a" is a new insertion at the end; adding "b" again changes nothing because it is already present.

More OOP with C++ and Java questions

Exams that ask this topic