posted 16 years ago
The correct answers are 1), 3), 5)
There are 2 requirements in the contract + one indication (it is appropriate to respect it)
a. to return the same hash code between calls if the object's state is not changed.
b. if x.equals(y) is true than it is required that x.hashCode() == y.hashCode()
c. if x.equals(y) is false than it is appropriate but not required that x.hashCode() != y.hashCode()
Back to the question:
a. is true for all 5 answers
b. is true only for 1), 3), 5),
1) because in any case x.hashCode()== y.hasCode()
3), 5) use commutative operations (a+b==b+a) ,( a^b = b^a) and so the b) is satisfied given this particular equals implementation
for example: ob1 = (1,2) , ob2= (2,1)
c) is not required
For '6)' return (a 16)|b; - the code will not compile
[ August 29, 2005: Message edited by: Balazs Borbely ]
'Make everything as simple as possible, but not simpler.' --Albert Einstein