Java OCA OCP Practice Question 1466

Question

Which statement is true about the code that can fill in the blank?

class MyClass { 
   public int hashCode() { 
      return 1; 
   } 
   public boolean equals(Object o) { 
      return                ; 
   } 
} 
  • A. It must return false.
  • B. It must return true.
  • C. It can return either true or false.
  • D. None of the above.


D.

Note

If two instances of a class have the same hash code, they might or might not be equal.

The reverse is not true.

If two objects are equal, they must have the same hash code in order to comply with the contracts of these methods.

The answer is none of the above because the method can't simply return true or false.

Based on the rules of equals(), if null is passed in, the result must be false.

If an object identity is passed in, the result must be true due to reflexivity.

Option D is correct.




PreviousNext

Related