← Quiz: Java Interview Questions: all 10 questions
Quiz: Java Interview Questions · Question 8 of 10
Why must two equal objects return the same hashCode()?
Why must two equal objects return the same hashCode()?
- A.So that toString() prints the same text
- B.So that they sort in the same order
- C.Otherwise HashMap and HashSet may look in the wrong bucket and fail to find an equal key
- D.Because equals() calls hashCode() internally
Answer & explanation
Correct answer: Otherwise HashMap and HashSet may look in the wrong bucket and fail to find an equal key
Hash collections pick the bucket by hashCode first and only then compare with equals.
Can you get the rest right without peeking?
Take the timed Quiz: Java Interview Questions quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →