Java: Which statement about equals() and hashCode() Contract is the strongest interview answer?

Difficulty:

Easy

Questions:

1

Time Limit:

2 minutes

Passing Score:

100%

Question

Which statement about equals() and hashCode() Contract is the strongest interview answer?

  1. If two objects are equal according to equals, they must return the same hashCode.
  2. equals() and hashCode() Contract is mostly a naming style choice and has little effect on runtime or design.
  3. equals() and hashCode() Contract exists mainly to reduce the number of Java files in a project.
  4. equals() and hashCode() Contract matters only for frontend frameworks, not for core Java applications.

Hint

Start with the core rule behind equals() and hashCode() Contract.

Answer and rationale

Correct answer: A. If two objects are equal according to equals, they must return the same hashCode.

If two objects are equal according to equals, they must return the same hashCode. This is the base concept interviewers commonly test first.

Track: Java