🎙 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 7 of 10

You put an object into a HashMap as a key, then change a field that its hashCode() uses. What usually happens on map.ge…

You put an object into a HashMap as a key, then change a field that its hashCode() uses. What usually happens on map.get(thatObject)?

  1. A.It finds the entry as usual
  2. B.The map updates the bucket automatically
  3. C.It returns null, because the new hash points to a different bucket than where the entry was stored
  4. D.It throws ConcurrentModificationException
Answer & explanation

Correct answer: It returns null, because the new hash points to a different bucket than where the entry was stored

This is why keys should be immutable (String, Integer, records with final fields).

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 →