Java OCA OCP Practice Question 1915

Question

Although Wrapper classes are not explicitly mentioned in the exam objectives,

we have seen some candidates get questions on this aspect of Wrapper classes.

What will be the output of the following program?

public class Main{
   public static void main (String args []){
      Integer i = new Integer(1) ;
      Long m = new Long(1);
      if ( i.equals (m))System.out.println ("equal");   // 1
      else System.out.println ("not equal");
    }
}

Select 1 option

  • A. equal
  • B. not equal
  • C. Compile time error at // 1
  • D. Runtime error at // 1
  • E. None of the above.


Correct Option is  : B

Note

Signature of equals method is: boolean equals (Object o); So it can take any object.

The equals methods of all wrapper classes first check if the two object are of same class or not.

If not, they immediately return false.

Hence it will print not equal.




PreviousNext

Related