Java OCA OCP Practice Question 2258

Question

How many of the following pairs of values can fill in the blanks to comply with the contract of the hashCode() and equals() methods?

class Main { //w  w w.  j a v  a2s  .  com
   @Override 
   public int hashCode() { 
      return  ___; 
   } 
   @Override 
   public boolean equals(Main o) { 
      return ___; 
   } 
} 
  • I. 5, false
  • II. 5, true
  • III. new Random().nextInt(), false
  • IV. new Random().nextInt(), true
  • A. None
  • B. One
  • C. Two
  • D. Three
  • E. Four
  • F. None of the above. The code does not compile with any of the options.


F.

Note

The equals() method in the Object class has a parameter of type Object.

An overridden version is required to have the same type.

The equals() method in Main is an overload rather than an override.

Since there is an @Override annotation, the code does not compile.




PreviousNext

Related