🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.Replaces the first key's value
  2. B.Stores both in the same bucket (a collision) and uses equals() to tell them apart
  3. C.Rejects the second key
  4. 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 →