Java OCA OCP Practice Question 1478

Question

Which of the following correctly fills in the blank so this code compiles and prints true?

public class Main { 
   private String text; 
   public int hashCode() { 
      return text.hashCode(); 
   } //from w w  w  . ja  v a 2 s.  c om
   public boolean equals(Object o) { 
      if (                  )  return false; 
      Main b = (Main) o; 
      return text.equals(b.text); 
   } 
   public static void main(String[] args) { 
      Main b1 = new Main(); 
      Main b2 = new Main(); 
      b1.text = "mickey"; 
      b2.text = "mickey"; 
      System.out.println(b1.equals(b2)); 
   } 
} 
  • A. (o instanceof Main)
  • B. (o instanceOf Main)
  • C. !(o instanceof Main)
  • D. !(o instanceOf Main)


C.

Note

There is no instanceOf keyword, making Options B and D incorrect.

There is an instanceof keyword.

If an object is the wrong type, the equals() method should return false, making Option C the answer.




PreviousNext

Related