Objects and Collections

Q1:
Which two statements are true about comparing two instances of the same class, given that the equals() and hashCode() methods have been properly overridden?
  1. If the equals() method returns true, the hashCode() comparison == must return true.
  2. If the equals() method returns false, the hashCode() comparison != must return true.
  3. If the hashCode() comparison == returns true, the equals() method must return true.
  4. If the hashCode() comparison == returns true, the equals() method might return true.

A 1 and 4

B 2 and 3

C 3 and 4

D 1 and 3

ANS:A - 1 and 4

(1) is a restatement of the equals() and hashCode() contract. (4) is true because if the hashCode() comparison returns ==, the two objects might or might not be equal. (2) and (3) are incorrect because the hashCode() method is very flexible in its return values, and often two dissimilar objects can return the same hash code value.



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.