Session length

1 / 20

Which statement accurately reflects the equals/hashCode contract?

If two objects are equal according to equals(), their hashCode must be equal

In Java, objects that are considered equal by the equals method must produce the same value from hashCode. This alignment is essential for hash-based collections like HashMap and HashSet to find and retrieve elements reliably. If two equal objects could have different hash codes, they would end up in different buckets, making lookups unpredictable or incorrect.

Hash codes are used for hashing, not for sorting. Sorting relies on comparisons (like compareTo or a Comparator), while hashing relies on the hashCode value to bucket objects.

If you override equals, you should also override hashCode to maintain this contract. It doesn’t matter whether the objects are mutable; what matters is consistency: objects that are equal must always yield the same hash code. If the fields involved in determining equality change after an object is placed in a hash-based collection, it can become unreachable or misplaced unless you carefully manage mutations.

If two objects are equal, their hashCode can be different

hashCode is used only for sorting

You must override hashCode only if objects are mutable

Next question

Find the option that is right for you!

All options are one-time payments.

$12.50

30 day premium pass

All the basics to get you started

  • Ad-free experience
  • View your previous attempt history
  • Mobile app access
  • In-depth explanations
  • 30 day premium pass access
$30.00 $87.50 usd

6 month DELUXE pass (most popular)

Everything with the 30 day premium pass FOR 6 MONTHS! & the ultimate digital PDF study guide (BONUS)

  • Everything included in the premium pass
  • $87.50 usd value for $30.00! You save $57.50!
  • + Access to the ultimate digital PDF study guide
  • + 6 months of premium pass access
  • + Priority support
$12.50 $18.99

Ultimate digital PDF study guide

For those that prefer a more traditional form of learning

  • Available for instant download
  • Available offline
  • Hundreds of practice multiple choice questions
  • Comprehensive content
  • Detailed explanations
Image Description
Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy