← Quiz: HashMap and Maps: all 10 questions
Quiz: HashMap and Maps · Question 9 of 10
Two different keys have the same hashCode() but equals() returns false. What does HashMap do?
Two different keys have the same hashCode() but equals() returns false. What does HashMap do?
- A.Replaces the first key's value
- B.Stores both in the same bucket (a collision) and uses equals() to tell them apart
- C.Rejects the second key
- D.Throws IllegalStateException
Answer & explanation
Correct answer: Stores both in the same bucket (a collision) and uses equals() to tell them apart
Equal hash codes only choose the same bucket; equals decides whether two keys are the same key.
Can you get the rest right without peeking?
Take the timed Quiz: HashMap and Maps quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →