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:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# Wrong Answer in Master Exam? [equals() and hashCode() Contract]

Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
There is a question in Master Exam A mock test:

For me the only correct answer is C, and MasterExam claims that correct answers are C and D. For me answer D is wrong, cause we can imagine the situation where we have two SortOf objects with the following instance variables values:

A:
length=2;
bal=3;
rate=4;

B:
length=2;
bal=4;
rate=3;

Given the equals method from D answer both object are considered equal, cause 2*3*4=2*4*3, but theirs hashCode differs cause 2*3 != 2*4, so the contract is not fulfilled. Is it rather obvious mistake in MasterExam or somehow i get it wrong?

Ranch Hand
Posts: 310
1
• Number of slices to send:
Optional 'thank-you' note:
I think C is the only correct answer!
For D to be correct the 'rate' has to be declared as a final variable

Mana Lesar
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Exactly.

 Consider Paul's rocket mass heater.