GeeCON Prague 2014*
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


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
Author

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:

Given:


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!


SCJP, SCWCD, SCBCD
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
SCJP questions and errors are usually discussed on another forum. Moving.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Doubt on a K & B LearnKey CD question