ProgrammingHardProduct-company placement · Java

A class overrides equals correctly, but its hashCode() returns 42 for every object. What happens when many such objects are stored in a HashMap?

  1. A. get() may return the wrong value for some keys
  2. B. The HashMap throws an exception for duplicate hash codes
  3. C. Lookups still give correct results, but every key lands in the same bucket, so performance degrades
  4. D. Only the first object is stored; the rest replace it

Correct answer: C. Lookups still give correct results, but every key lands in the same bucket, so performance degrades

A constant hashCode obeys the contract (equal objects have equal hash codes), so correctness is preserved; equals still separates the keys. All entries collide, turning O(1) lookups into list or tree scans.

More OOP with C++ and Java questions

Exams that ask this topic