aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt about MasterExam question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "doubt about MasterExam question" Watch "doubt about MasterExam question" New topic
Author

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 :
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

Joined: Mar 10, 2008
Posts: 1402

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: doubt about MasterExam question
 
Similar Threads
Wrong Answer in Master Exam? [equals() and hashCode() Contract]
Doubt-MasterExam-Hashcode
About equals() and hashcode() contracts
A question from Mock Test about hashCode
HashCode and Equals