• 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

doubt about MasterExam question

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here there is the question in the MasterExam:


Which of the following will fulfill the equals() and hashCode() contracts for this class?
(Choose all that apply)

A: return ((SortOf)o).bal == this.bal;

B: return ((SortOf)o).code.length() == this.code.length();

C: return ((SortOf)o).code.length()*((SortOf)o).bal == this.code.length();

D: return ((SortOf)o).code.length()*((SortOf)o).bal*((SortOf)o).rate == this.code.length()*this.bal*this.rate;

ANSWER : C and D

I'm not agree with D because i made an example where
SortOfObject1.equals(SortOfObject2) and SortOfObject1.hashCode()!=SortOfObject2.hashCode()

My example:


the output is :
true
15
30


Maybe I'm wrong with something, please tell me.

Thank you
[ March 12, 2008: Message edited by: Alain Sellerin ]
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Technically none of them do, as for any null reference that is passed as an argument to equals(), instead of returning false as the contract for equals specifies, these implementations all throw a NullPointerException. As the contract for equals() is never completely satisfied for any implementation, I'd say the question is flawed.
[ March 12, 2008: Message edited by: Jelle Klap ]
 
Niala Nirell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for your comment
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic