wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt about MasterExam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt about MasterExam question" Watch "doubt about MasterExam question" New topic

doubt about MasterExam question

Niala Nirell
Ranch Hand

Joined: Mar 12, 2008
Posts: 46
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 :

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

Thank you
[ March 12, 2008: Message edited by: Alain Sellerin ]
Jelle Klap

Joined: Mar 10, 2008
Posts: 1951

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 ]

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Niala Nirell
Ranch Hand

Joined: Mar 12, 2008
Posts: 46
thank you for your comment
I agree. Here's the link: http://aspose.com/file-tools
subject: doubt about MasterExam question
It's not a secret anymore!