Skip to content
Sharp

Core Java · interview question

When are two objects considered equal in a Java Set?

Multiple choiceEasychapter-4-curatedpremiumcore-javaequals-and-hashcode-in-collections
Choose one answer
Answer and explanation

Correct answer

B. When their hashCodes are equal and they are equal according to equals.

Why

In a Set, two objects are considered equal if they have the same hashCode and are equal according to the equals method. This ensures the unique constraint of a Set.

Test yourself on Core Java

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

Start free test
All Core Java questions →