LearnKey Bonus Exam question
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Maybe I have overlooked something, but the results of this question is somehow unclear to me.
Question
Answers
Stated correct answer is A. But how can be 2 objects having different hashCodes equal? This violates the requirement that if o1.equals(o2) == true then o1.hashCode() == o2.hashCode().
Is there a bug or have I overlooked something? Thanks for help.
SCJP 1.5 (done, at last)<br /> <br />"If everything seems to be going well you have obviously overlooked something."
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
what is x1 to x7??
SCJP 5
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
because the output is "x=1111", so all boolean expression in the question are true
CMIIW
If two objects are equal according to the equals(Object) method, then
calling the hashCode() method on each of the two objects must produce the
same integer result.
from :
x1.hashCode()!=x2.hashCode()
check:
(A) x2.equals(x1) == true
Because x1.hashCode()!=x2.hashCode() means x2.equals(x1) == false. If they're equals the should have same hashcode result. So option A is wrong
from:
x3.equals(x4) == false
check:
(B) x3.hashCode() != x4.hashCode()
We cannot determine whether their hashcode is equal or not, because if 2 object are not equals they can have same or different hashcode. And that not violating the contract. So option B also wrong
from:
x5.equals(x6) == true
check:
(C) x5.hashCode() == x6.hashCode()
Their equals so that mean their hashcode should be equal too
. So C is right.
from :
x7.hashCode() == x8.hashCode()
check:
(D) x8.equals(x7) == true
Even their hashcode is equal we cannot say the objects also equals. because it's possible and not violating the contract for 2 un-equal object having same hashcode. So D is not correct.
It is NOT required that if two objects are unequal according to the
equals(java.lang.Object) method, then calling the hashCode() method
on each of the two objects must produce distinct integer results. However,
the programmer should be aware that producing distinct integer results for
unequal objects may improve the performance of hashtables.
[ December 04, 2006: Message edited by: jimmy halim ]
Knowledge is power<br />My Blog : <a href="http://jimmod.blogspot.com/" target="_blank" rel="nofollow">http://jimmod.blogspot.com/</a>
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I did not think it is possible but as we see nothing is perfect.
Thanks.
SCJP 1.5 (done, at last)<br /> <br />"If everything seems to be going well you have obviously overlooked something."
| A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |






