Java OCA OCP Practice Question 1496

Question

Which of the following values can fill in the blank for the class to be correctly implemented?

class MyClass { 
  public int hashCode(Object o) { 
     return                    ; 
  } 
  public boolean equals(Object o) { 
     return true; 
  } 
} 
  • I. -1
  • II. 5
  • III. new Random().nextInt()
  • A. I
  • B. I and II
  • C. I, II, and III
  • D. I and III


C.

Note

The hashCode() method in the Object class does not have a parameter.

The MyClass class provides an overloaded method rather than an overridden one.

Since it is not an overridden method, the contract for the Object class' hashCode() method does not apply, and any int value can be returned.

Option C is correct.




PreviousNext

Related