Skip to content
Sharp

Object-oriented programming · interview question

Which statements about the equals/hashCode contract are true? Select all that apply.

Select all that applyMedium
Select all that apply
Answer and explanation

Correct answer

  • A. If a.equals(b) is true, then a.hashCode() == b.hashCode() must be true.
  • C. Overriding equals without hashCode breaks lookups in HashMap and HashSet.
  • D. equals must be symmetric: a.equals(b) implies b.equals(a).

Why

Hash-based collections find the bucket by hashCode, then compare with equals. Equal objects with different hash codes land in different buckets and can't be found.

References

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →