• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Q14 practice exam 1 from OCP JP practice exams

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should ask this the JP forum not here.
 
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks. i got it.
 
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 63
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic