Java OCA OCP Practice Question 661

Question

Assume that a, b, and c refer to instances of primitive wrapper classes.

Which of the following statements are correct?

Select 2 options

  • A. a.equals(a) will always return true.
  • B. b.equals(c) may return false even if c.equals(b) returns true.
  • C. a.equals(b) returns same as a == b.
  • D. a.equals(b) throws an exception if they refer to instances of different classes.
  • E. a.equals(b) returns false if they refer to instances of different classes.


Correct Options are  : A E

Note

Equals method of a primitive wrapper class are

symmetric => a.equals(b) returns same as b.equals(a) 

transitive => if a.equals(b) and b.equals(c) return true, then a.equals(c) returns true. 

reflexive => a.equals(a) return true. 

For example, the following code for the equals method on Integer explains how it works:

public boolean equals (Object obj)  { 
   if  (obj instanceof Integer)  { 
       return value ==  ((Integer)obj).intValue (); 
    } 
   return false; 
} 



PreviousNext

Related