aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mock question: hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "mock question: hashcode" Watch "mock question: hashcode" New topic
Author

mock question: hashcode

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
Consider the following code:
1. class Car {
2. String make;
3. String model;
4. //Insert code here.
5. public boolean equals(Object o) {
6. Car c = (Car) o;
7. return model.equals(c.model);
8. }
9. }
Which of the following inserted independently at line 4 will honor the hashCode contract?

A. public int hashCode () {return (int) (Math.random()*101);}
B. public int hashCode () {return 420;}
C. public int hashCode () {return make.hashCode();}
D. public int hashCode () {return model.hashCode();}
E. None of the above

















----------------
answers: B and D


I dont understand at all, why B and D??
if we return 420 for all objects, how does it practically honor the hashcode contract?

and model.hashCode(), is this even valid and wats the logic here?

pls help!
Mehmet Gursul
Greenhorn

Joined: Jan 26, 2006
Posts: 6
If two objects are equal then their hashCode() should also be equal.

So, although returning constant value (420 or any other value), is not a good thing, but it is legal. B and D are correct answers.


Mehmet Gursul
SCJP 5.0 (81%)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

Originally posted by adam lui:
and model.hashCode(), is this even valid and wats the logic here?

Note that hashCode() is a method in class Object. So you can call hashCode() on any object. 'model' is a String, so calling hashCode() on it is legal.

The hashCode() contract says: If two objects are equal (which means: equals() returns true when comparing the two objects), then the hash codes of the two objects should also be equal.

Note that this does not mean that the hash codes must be different if the two objects are not equal!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
Originally posted by Jesper Young:

Note that hashCode() is a method in class Object. So you can call hashCode() on any object. 'model' is a String, so calling hashCode() on it is legal.

The hashCode() contract says: If two objects are equal (which means: equals() returns true when comparing the two objects), then the hash codes of the two objects should also be equal.

Note that this does not mean that the hash codes must be different if the two objects are not equal!


so for answer C, var "make" is also String, why is it incorrect?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

Originally posted by adam lui:
so for answer C, var "make" is also String, why is it incorrect?

It's not only about if it compiles correctly or not.

Look closely again at the equals() method in class Car. It compares two Car objects only by looking at 'model'. So two Car objects are considered equal if their 'model' member variables are equal.

Now remember the hashCode() contract: if two objects are equal, their hash codes must be equal. With answer C, that is not always the case, so answer C is wrong. You can have two Car objects with the same value in 'model' but different values in 'make'. The hashCode() contract would be violated in case of answer C.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: mock question: hashcode