• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

LearnKey Bonus Exam question

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody.
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.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
don't understand the code. looks vague for me.
what is x1 to x7??
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMO the right answer is C
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 ]
 
Peter Laurinec
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it is as I thought. There is a bug in LearnKey Bonus Exam answer.
I did not think it is possible but as we see nothing is perfect.

Thanks.
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic