Java OCA OCP Practice Question 755


What will the following program print?

public class Main{ 
  public static void main (String [] args){ 
     Object obj1 = new Object (); 
     Object obj2 = obj1; 
     if ( obj1.equals (obj2) ) System.out.println ("true"); 
     else  System.out.println ("false"); 

Select 1 option

  • A. true
  • B. false
  • C. It will not compile.
  • D. It will compile but throw an exception at run time.
  • E. None of the above.

Correct Option is  : A


Object class's equals () method just checks whether the two references are pointing to the same location or not.

In this case they are pointing to the same location because of obj2 = obj1; so it returns true.