Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

K&B Master Exam Question 56

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i think the answer should be only C
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Sylvia Trommer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...
 
ice is for people that are not already cool. Chill with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic