Skip to content
Sharp

Core Java · interview question

Given a TreeSet storing product IDs, why might a new product ID not be added even if it appears to be unique?

Multiple choiceHardchapter-4-curatedpremiumcore-javacode-reading-with-collections
Choose one answer
Answer and explanation

Correct answer

C. Product ID is not comparable.

Why

TreeSet requires elements to be either naturally ordered or provided with a Comparator. If product IDs are not comparable, the TreeSet cannot determine where to insert the new ID, causing the addition to fail.

Test yourself on Core Java

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

Start free test
All Core Java questions →