Forums Register Login

K&B Master Exam Question 56

+Pie Number of slices to send: Send
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());
}
+Pie Number of slices to send: Send
yeah i think the answer should be only C
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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....
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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 think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 953 times.
Similar Threads
Wrong Answer in Master Exam? [equals() and hashCode() Contract]
Question on hashCode() & equals() from master exam 1
hashcode & equals question
A question from Mock Test about hashCode
hashcode contract S&B question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:21:16.