• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mana Lesar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic