Courseiva
Working with Arrays and CollectionsmediumMultiple ChoiceObjective-mapped

1Z0-829 Working with Arrays and Collections Practice Question

A team uses a TreeSet with a custom Comparable that returns 0 for objects that are not logically equal (e.g., based on one field but objects differ in another). What is the likely outcome when adding such objects?

⚠ Common exam trap

Many exam-takers assume TreeSet uses equals() for duplicate detection, but it actually uses compareTo()/compare(), so objects that are not logically equal can be incorrectly treated as duplicates if the comparison method returns 0.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

The second object is not added because TreeSet considers it a duplicate

TreeSet uses the compareTo() method of the Comparable interface (or Comparator) to determine ordering and equality. When compareTo() returns 0 for two objects that are not logically equal (e.g., they share the same field used in comparison but differ in other fields), TreeSet treats them as duplicates and does not add the second object. This is because TreeSet relies on the comparison result, not the equals() method, for uniqueness.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • The second object is not added because TreeSet considers it a duplicate

    Why this is correct

    TreeSet uses compareTo for equality; returning 0 means duplicate.

  • The second object replaces the first

    Why it's wrong here

    TreeSet does not replace; it rejects duplicates.

  • A ClassCastException is thrown

    Why it's wrong here

    compareTo is defined, so no ClassCastException.

  • Both objects are added and insertion order is preserved

    Why it's wrong here

    TreeSet sorts, not preserves order, and duplicate detection prevents adding second.

About these practice questions

One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.