aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Master Exam Question 56 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 "K&B Master Exam Question 56" Watch "K&B Master Exam Question 56" New topic
Author

K&B Master Exam Question 56

Tyronne Fernando
Ranch Hand

Joined: May 11, 2008
Posts: 50
The Question 56 is:

Given:

class SortOf {
String name;
int bal;
String code;
short rate;

public int hashCode() {
return (code.length() * bal);
}

public boolean equals(Object o) {
// insert code here
}
}

Which of the following will fullfil the equals() and hashCode() contracts for this class? (Choos 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() * this.bal;
D return ((SortOf)o).code.length() * ((SortOf)o).bal * ((SortOf)o).rate ==
this.code.length() * this.bal * this.rate;

C and D are given as the correct answers.

I agree C is correct. But, If I create the SortOf class with answer D and run the following code, I get true and false. So, the hashCode() returns a different value when the objects are equal. Does that mean the correct answer is C only?

Thanks.

public static void main(String[] args) {

SortOf one = new SortOf();
SortOf two = new SortOf();

one.code = "a";
one.bal = 2;
one.rate = 3;

two.code = "abc";
two.bal = 2;
two.rate = 1;

System.out.println(one.equals(two));
System.out.println(one.hashCode() == two.hashCode());
}
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 122
yeah i think the answer should be only C


SCJP 6 83%
Working on SCWCD/OCPJWCD
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
If i'm not wrong there is already a topic talking about it here. And if you have a proof of equals==equals and hashCode!=hashCode so the question is wrong.


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Sylvia Trommer
Greenhorn

Joined: Aug 21, 2008
Posts: 6
I came to this forum to find an answer to the exact same question - I'm happy to read that it's not me being crazy, but that there is actually a wrong answer on the cd.
I only hope that there are no such mistakes on the real exam
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Raphael Rabadan is right...the answer is C and D...

The contract between equals and hashCode says that for equal objects the hashCode must be equal

in option D the equals method considers rate while finding whether two objects are equal.

soppose two objects have same code.length() and bal but different rate. then equals will return false and hashCode will return equal value. but this doesn't break the contract between equals and hashCode....hashCode can return equal value for unequal objects...infact you can implement hashCode like this
public void hashCode()
{
return 123;
}

this is perfectly legal for the contract between equals and hashCode....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sylvia Trommer
Greenhorn

Joined: Aug 21, 2008
Posts: 6
what you are saying is right, but you're missing one thing - like in the example in the first post - the equals method can return true while the hash code doesn't, which breaks the contract.

Suppose code.length is the same for two objects, and bal and rate are swapped. So for object a, code.length is 1, bal is 2 and rate is 3, and for object b, code.length is 1, bal is 3 and rate is 2.

Equals would return true (1*2*3 == 1*3*2) while hashcode comparison would return false (1*2 != 1*3).
John Sutt
Ranch Hand

Joined: Mar 07, 2008
Posts: 42
Ankit, you have it wrong, answer D is not correct here.
The only correct answer is answer C.
I had the same problem, and figured out it has been posted several
times in this forum (search the forum for SortOf, if you want).

Tyronne, your example indeed proves the hashCode contract is violated.
There are many other examples, where you take rate == 0 for two instances, they're always equal then, but most don't have the same hashCode.


SCJP 6 (91%), SCJD (91%)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Originally posted by John Sutt:

There are many other examples, where you take rate == 0 for two instances, they're always equal then, but most don't have the same hashCode.


Ya, I never thought about rate being 0.....thanks folks.....This has really opened my eyes about how confusing some questions might get...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Master Exam Question 56