Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q14 practice exam 1 from OCP JP practice exams

 
binay shah
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If instances of class Chilis are to be used as keys in a Map, which are true?
A. Without overriding hashCode(), the code will not compile.
B. As it stands, the equals() method has been legally overridden.
C. It's possible for such keys to find the correct entries in the Map.
D. It's not possible for such keys to find correct entries in the Map.
E. As it stands, the Chilis class legally supports the equals() and hashCode() contracts.
F. If hashCode() was correctly overridden, it would make retrieving Map entries by key easier.

The correct answer is B, C and E.
I don't understand why C is correct. According to me, instead D should be correct because equals method is not correctly overridden.
Can anyone explain this to me? thanks in advance.
 
K. Tsang
Bartender
Posts: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should ask this the JP forum not here.
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
binay shah wrote: instead D should be correct because equals method is not correctly overridden.
Can anyone explain this to me? thanks in advance.


Why do you think it isn't correctly overridden ?

When they're comparing objects, toString() is being called.
 
Himai Minh
Ranch Hand
Posts: 1232
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
binay shah wrote:
C. It's possible for such keys to find the correct entries in the Map.
I don't understand why C is correct. According to me, instead D should be correct because equals method is not correctly overridden.
Can anyone explain this to me? thanks in advance.


Consider this example:
 
binay shah
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. i got it.
 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Opera Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, everyone, going to step in in order to ask for a doubt

Something I don't understand yet, as solution says :

E. As it stands, the Chilis class legally supports the equals() and hashCode() contracts.

Is this correct besides it has not been overriden hashCode()? (Just equals). Being a Map doesn't need for override both, equals() and hashCode()? . What am I missing? :S

Thanks in advance .
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Samer wrote:Hello, everyone, going to step in in order to ask for a doubt

Something I don't understand yet, as solution says :

E. As it stands, the Chilis class legally supports the equals() and hashCode() contracts.

Is this correct besides it has not been overriden hashCode()? (Just equals). Being a Map doesn't need for override both, equals() and hashCode()? . What am I missing? :S

Thanks in advance .


That's why option E is incorrect.
 
Himai Minh
Ranch Hand
Posts: 1232
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Samer wrote:Hello, everyone, going to step in in order to ask for a doubt

Something I don't understand yet, as solution says :

E. As it stands, the Chilis class legally supports the equals() and hashCode() contracts.

Is this correct besides it has not been overriden hashCode()? (Just equals). Being a Map doesn't need for override both, equals() and hashCode()? . What am I missing? :S

Thanks in advance .


Hello David,
The contract is:
1. two objects are equal meaning they have the same hashcode.
2. two objects are not equal meaning they may or may not have the same hashcode.
3. two objects have the same hashcode meaning they may or may not equal.
4. two objects have different hashcodes meaning they are not equal.

Chilis does not override the hashCode() method. So, Chilis inherits the hashCode() from Object class. Therefore,
1. Each Chilis instance has a unique hashcode.
2. In your Chilis class, it defines "equals" means "o refers to the same Chilis instance".

Does your Chilis class violate the 4 conditions of the contract? No.
Reason 1: o refers to the same "this" chilis meaning they have the same unique hashcode.
Reason 2: If o does not refer to the same "this" chilis instance meaning they have different hashcode.
Reason 3: If o and the "this" Chilis have the same hashcode meaning they are equal.
Reason 4: If o and the "this" Chilis have different hashcode meaning they are not equal.

Is it possible to have two Chilis instance with different unique hashcodes, but they are the same objects? No. I don't see this from the code.
 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Opera Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hello there Himai

Himai Minh wrote:

Is it possible to have two Chilis instance with different unique hashcodes, but they are the same objects? No. I don't see this from the code.


Is it possible? Wouldn't be the same instance due equals and inherited hasCode()?

Then, mainly because hashCode() is inherited from Object class, it means it fulfills its contract?

Thanks in advance
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Samer wrote:

Hello there Himai

Himai Minh wrote:

Is it possible to have two Chilis instance with different unique hashcodes, but they are the same objects? No. I don't see this from the code.


Is it possible? Wouldn't be the same instance due equals and inherited hasCode()?

Then, mainly because hashCode() is inherited from Object class, it means it fulfills its contract?

Thanks in advance


Contract fulfills only when you do @override.
 
Niraj Jha
Ranch Hand
Posts: 63
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
must remember :-

If we override equals()(in user defined key class) then must override hashCode(), otherwise it has no effect

e.g

import java.util.*;
class Chilis{
static int t1 = 0;
Chilis(String c, int h){ color = c; hotness = h;}
String color;
int hotness;
public boolean equals(Object o){
System.out.println("inside equals");
Chilis ch = (Chilis)o;
if(this.hotness == ch.hotness ) return true;
return false;
}
public int hashCode(){

hotness = hotness + t1;
System.out.println("inside hashcode" + hotness);
t1= t1+10;
return hotness;
}
public String toString(){
return color + " " + hotness;
}

public static void main(String asd[]){
Set m1 = new HashSet();
m1.add(new Chilis("red",10));
m1.add(new Chilis("red",10));
System.out.println(m1);
}
}

output:

inside hashcode10
inside hashcode20
[red 20, red 10]

 
Himai Minh
Ranch Hand
Posts: 1232
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Samer wrote:


Himai Minh wrote:

Is it possible to have two Chilis instance with different unique hashcodes, but they are the same objects? No. I don't see this from the code.


Is it possible? Wouldn't be the same instance due equals and inherited hasCode()?

Then, mainly because hashCode() is inherited from Object class, it means it fulfills its contract?

Thanks in advance


Hi David,
Two Chilis instances have different hashcodes in this case. So, these two instances are not equal.
 
Anand Reng
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I am understanding this is - The equals method compares this object and the
chillis object passed, and it returns true not because the references are same, but
their string values are same as toString method is called. So any two Chillis with the
same color and hotness are equal. Since they are two different objects, each have their
own hashcode and thus hashcode overriding is required?

Please correct me if this is wrong.
 
Himai Minh
Ranch Hand
Posts: 1232
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anand,
It is not required to override hashcode() method.

You also also try this:


This still works. All Chilis instances have the same hashCode. If they are not equal based on how you define the equal () method, it won't violate the 4 conditions of the hash contract.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic