This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Enthuware mock test question (hashCode)

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
My queries is,
Consider the following class:

public class X
{
private int a;
private int b;
public void setA(int i){ this.a = i; }
public int getA(){ return this.a; }
public void setB(int i){ this.b = i; }
public int getB(int b){ return b; }
public boolean equals(Object obj)
{
return ( obj instanceof X && this.a == ((X) obj).a );
}
public int hashCode()
{
//1
}
}

Which of the following options would be valid at //1?

a. return 0;
b. return a;
c. return a+b;
d. return a*a;
e. return a/2;

answer is a,b,d and e.

Is it because of the equals only evaluate on object a. Therefore, hashCode should not evaluate object b? I got confused. please help.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the result of (a+b) is not depended on a . It is depended on result of a+b. Where equals method is not depended on b but a.
So when we comparing two elements by hashcode even when the value of a is same in both objects but differ in value of b. Then equals metheod will return true but hashcode will be different.So that the option c is not valid.
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel that e is also not a good implementation of hashCode(it is correct though). That's because if a is 0, then it will throw an ArithmeticException...
 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can take it as Scenario below?

obj1: a=1, b=2
obj2: a=1, b=3

if hashcode return a+b, Therefore, hashcode for obj1 is 3(1+2) wherea obj2 is 4(1+3)? and caused equals return true but hashcode will be different.

However, option a,b,d,e will return same value in hashcode. But a is bad hashcode because of 0 could be too common among object comparison?

I am getting what about your explanation.
 
Greenhorn
Posts: 13
Netbeans IDE Opera Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ankit Garg:
I feel that e is also not a good implementation of hashCode(it is correct though). That's because if a is 0, then it will throw an ArithmeticException...



How can 0(zero) / 2 (or any other number) throw an ArithmeticException ? returning 0 in hashCode() method isn't good either, yet it is listed as correct answer...
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does valid means legal or appropriate because when i run program with option c ie return a+b, program seems to work fine. In my main i am inserting two instances of class X in HashSet.

 
Enthuware Software Support
Posts: 4588
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a detailed explanation provided with the answers -


Rule: If the equals() method returns true, then hashCode() for both the objects must return the same value. [Note: The reverse is not required.]

a. return 0
Although it is a very bad hashCode but it satisfies the rule.

b return a
The equals method is only testing the value of a, so returning the value of a in hashCode() is a valid option

c. return a+b
Not valid because two object will be equal if their a values are same, but they will return different hashcodes, if their b values are different.

 
Ankit Garg
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vaes Bart:

How can 0(zero) / 2 (or any other number) throw an ArithmeticException ?



 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to run the code which Ankit has posted. It gives 0.
 
Ankit Garg
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Everyone. I messed up here. It's my fault. 0 / 2 will not give ArithmeticException. I don't know what I was thinking ... Sorry again...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic