Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt about MasterExam question

 
Niala Nirell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here there is the question in the MasterExam:


Which of the following will fulfill the equals() and hashCode() contracts for this class?
(Choose all that apply)

A: return ((SortOf)o).bal == this.bal;

B: return ((SortOf)o).code.length() == this.code.length();

C: return ((SortOf)o).code.length()*((SortOf)o).bal == this.code.length();

D: return ((SortOf)o).code.length()*((SortOf)o).bal*((SortOf)o).rate == this.code.length()*this.bal*this.rate;

ANSWER : C and D

I'm not agree with D because i made an example where
SortOfObject1.equals(SortOfObject2) and SortOfObject1.hashCode()!=SortOfObject2.hashCode()

My example:


the output is :
true
15
30


Maybe I'm wrong with something, please tell me.

Thank you
[ March 12, 2008: Message edited by: Alain Sellerin ]
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically none of them do, as for any null reference that is passed as an argument to equals(), instead of returning false as the contract for equals specifies, these implementations all throw a NullPointerException. As the contract for equals() is never completely satisfied for any implementation, I'd say the question is flawed.
[ March 12, 2008: Message edited by: Jelle Klap ]
 
Niala Nirell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your comment
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic