Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# K&B Master Exam Question 56

Tyronne Fernando
Ranch Hand
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
Posts: 122
yeah i think the answer should be only C

Ranch Hand
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.

Sylvia Trommer
Greenhorn
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
Posts: 9542
33

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

Sylvia Trommer
Greenhorn
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
Posts: 42
Ankit, you have it wrong, answer D is not correct here.
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.

Ankit Garg
Sheriff
Posts: 9542
33
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...