File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q14 practice exam 1 from OCP JP practice exams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q14 practice exam 1 from OCP JP practice exams" Watch "Q14 practice exam 1 from OCP JP practice exams" New topic
Author

Q14 practice exam 1 from OCP JP practice exams

binay shah
Greenhorn

Joined: Jan 19, 2013
Posts: 14

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

Joined: Sep 13, 2007
Posts: 2247
    
    7

You should ask this the JP forum not here.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

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.


My one regret in life is that I am not someone else.
- Woody Allen
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 724
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

Joined: Jan 19, 2013
Posts: 14
thanks. i got it.
David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

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

Joined: Dec 05, 2012
Posts: 305

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

Joined: Jul 29, 2012
Posts: 724
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

Joined: Feb 08, 2012
Posts: 49



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

Joined: Dec 05, 2012
Posts: 305

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

Joined: Feb 20, 2013
Posts: 63

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

Joined: Jul 29, 2012
Posts: 724
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

Joined: Feb 11, 2013
Posts: 8
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

Joined: Jul 29, 2012
Posts: 724
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Q14 practice exam 1 from OCP JP practice exams
 
Similar Threads
S & B SCJP 6 - C7 - Q4 erratum
Doubt on PRACTICE EXAMS - OCP JAVA By kathy sierra and barty bates book about equals and hashcode
hashcode and equals not matching
equals and hashCode
Duplicate keys in Map?