QuizzeshashCode and equals Contract — Concept Check
intermediate
java

hashCode and equals Contract — Concept Check

Questions on hashCode and equals Contract. Correctly implementing hashCode and equals is critical for objects used as HashMap keys or in HashSets — violating the contract causes subtle bugs.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on hashCode and equals Contract — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.If two instances of a class return true for equals(), what must also be true?

    • AThey must be identical objects.
    • BThey must have the same hashCode.
    • CThey must be in the same memory location.
    • DThey must be in the same HashMap bucket.
  2. 2.A developer overrides equals() but not hashCode() in a class used as a HashMap key. What is a likely issue?

    • AThe map will automatically fix the issue.
    • BKeys may not be found in the map.
    • CThe map will throw an exception.
    • DThe map will perform faster.
  3. 3.Which property is NOT required for a correct equals() implementation?

    • AConsistency
    • BSymmetry
    • CHash collision avoidance
    • DTransitivity
  4. 4.In Java, what happens if a hashCode() method always returns the same constant?

    • AKeys are evenly distributed.
    • BHashMap performance improves.
    • CHashMap throws an error.
    • DAll keys go to the same bucket.
  5. 5.A HashSet contains an object. You modify the object’s fields and check if the set still contains it. What happens?

    • AThe set will find the object.
    • BThe set may not find the object.
    • CThe set will throw an exception.
    • DThe set will automatically update.
  6. 6.What is the result of calling hashCode() on two equal objects without overriding hashCode()?

    • ADifferent hashCodes, likely.
    • BHashSet will merge them.
    • CSame hashCodes, guaranteed.
    • DHashMap error occurs.
  7. 7.Why is using Objects.hash() recommended for hashCode implementations?

    • AIt combines fields cleanly and consistently.
    • BIt avoids all hash collisions.
    • CIt guarantees unique hashCodes.
    • DIt automatically fixes equals() issues.
  8. 8.What is a common mistake when implementing hashCode() for a class with multiple fields?

    • AUsing all fields for hashCode.
    • BUsing a prime number in hashCode.
    • CNot implementing equals().
    • DUsing only one field for hashCode.