🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Immutability and Value Objects: all 10 questions

Quiz: Immutability and Value Objects · Question 4 of 10

Why is it important to override hashCode whenever you override equals?

Why is it important to override hashCode whenever you override equals?

  1. A.The compiler refuses to compile otherwise
  2. B.Equal objects must have equal hash codes, or HashSet and HashMap will look in the wrong bucket
  3. C.hashCode makes equals run faster
  4. D.It is only needed for records
Answer & explanation

Correct answer: Equal objects must have equal hash codes, or HashSet and HashMap will look in the wrong bucket

Hash-based collections use hashCode first, then equals.

Can you get the rest right without peeking?

Take the timed Quiz: Immutability and Value Objects quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →