Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Enthuware mock test question (hashCode)  RSS feed

 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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.
 
subhasish nag
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Vaes Bart
Greenhorn
Posts: 13
Netbeans IDE Opera Suse
  • Mark post as helpful
  • send pies
  • 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...
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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.

 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 4096
33
  • Mark post as helpful
  • send pies
  • 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: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I tried to run the code which Ankit has posted. It gives 0.
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!