You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?

  1. java.util.Map

  2. java.util.Set

  3. java.util.List

  4. java.util.Collection


Correct Option: B

AI Explanation

To answer this question, you need to understand the different interfaces provided by Java's java.util package for storing collections of elements.

Option A) java.util.Map - This option is incorrect because a Map does not guarantee that no duplicates are stored. It allows for key-value pairs, where each key must be unique, but the values can be duplicated.

Option B) java.util.Set - This option is correct because a Set interface provides the capability of storing elements in a collection without allowing duplicates. It ensures that every element in the set is unique. Elements in a set are not stored in any particular order, but they can be accessed in a natural order.

Option C) java.util.List - This option is incorrect because a List interface allows duplicate elements to be stored. It maintains the insertion order of elements, which means elements can be accessed in the order they were added.

Option D) java.util.Collection - This option is incorrect because Collection is not a specific interface that provides the capability mentioned in the question. It is a more general interface that provides basic operations for collections, but it does not guarantee uniqueness or ordering.

The correct answer is B) java.util.Set. This option is correct because a Set interface guarantees that no duplicates are stored and allows for accessing elements in a natural order.

Find more quizzes: