Difficulty: Medium
Correct Answer: x3.hashCode() == x4.hashCode()
Explanation:
Introduction / Context:
The problem requires deducing which relationship must hold given four conditional increments produced the total 1111. Each increment corresponds to one Boolean condition being true. With equals() and hashCode() implemented correctly, we use the contract to infer implications.
Given Data / Assumptions:
Concept / Approach:
The equals–hashCode contract guarantees: if a.equals(b)
is true, then a.hashCode() == b.hashCode()
must be true. The converse is not required. If a.equals(b)
is false, there is no guarantee about hash codes (they may be equal or different).
Step-by-Step Solution:
Verification / Alternative check:
Construct sample objects to satisfy all four conditions; only the relation between x3 and x4 is logically forced both in equals and hashCode dimensions.
Why Other Options Are Wrong:
Common Pitfalls:
Believing unequal objects must have different hash codes; collisions are allowed. Also, thinking equal hash codes force equals() to be true (they do not).
Final Answer:
x3.hashCode() == x4.hashCode()
Discussion & Comments