Multiple choice technology programming languages

How does the set collection deal with duplicate elements?

  1. An exception is thrown if you attempt to add an element with a duplicate value.

  2. The add method returns false if you attempt to add an element with a duplicate value.

  3. A set may contain elements that return duplicate values from a call to the equals method.

  4. Duplicate values will cause an error at compile time.

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Java's Set collections prevent duplicates by using the add method. Instead of throwing an exception, Set.add() simply returns false if the element is already present, indicating that the collection was not modified. It does not produce compile-time or runtime errors for duplicate additions.