wood burning stoves
The moose likes Mock Exam Errata and the fly likes Doubt on a K & B LearnKey CD question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Doubt on a K & B LearnKey CD question" Watch "Doubt on a K & B LearnKey CD question" New topic

Doubt on a K & B LearnKey CD question

Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

Dear All,
This is a question from LearnKey CD provided along with K & B SCJP Guide:


Which of the following fulfill the equals() & hashCode() contract for this class? (Choose all that apply)

Correct Answer given are:

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

The explanation given is that the equals method must be atleast as precise as the hashCode() method.

The first option ofcourse is correct; but I don't understand how the second option is correct. I mean doesn't that break the hashCode - equals contract for cases in which code.length() of both objects compared are equal & the values of bal & rate for both are interchanged? In such cases this.hashCode() != o.hashCode(), but this.equals(o) will be true right? Confused!
Please help!

Rob Spoor

Joined: Oct 27, 2005
Posts: 19908

Please read http://www.coderanch.com/t/253306/Programmer-Certification-SCJP/certification/equls-hashCode-MasterExam-possible-error

How To Ask Questions How To Answer Questions
Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

Thanks Rob!

Good to know I also am not on the wrong side

There are more errors in the CD questions, which I don't know whom to mail/message to; but this one I thought it's something which I overlooked.

Thanks again!
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 41079
SCJP questions and errors are usually discussed on another forum. Moving.
I agree. Here's the link: http://aspose.com/file-tools
subject: Doubt on a K & B LearnKey CD question