🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: How Hashing Works: all 13 questions

Quiz: How Hashing Works · Question 12 of 13

Why must you not change a field used by hashCode() while the object is inside a HashSet?

Why must you not change a field used by hashCode() while the object is inside a HashSet?

  1. A.Java throws an exception at once
  2. B.The set automatically removes it
  3. C.The object stays in the bucket for its old hash, so lookups with the new hash look in the wrong bucket
  4. D.It makes the set sorted
Answer & explanation

Correct answer: The object stays in the bucket for its old hash, so lookups with the new hash look in the wrong bucket

The object becomes impossible to find or remove, even though it is still stored.

Can you get the rest right without peeking?

Take the timed Quiz: How Hashing Works quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →