Java OCA OCP Practice Question 328

Question

What is the output of the following application?


public class MyClass { 
   public static void main(String... data) { 
      String john = "john"; 
      String jon = new String(john); 
      System.out.print((john==jon)+" "+(john.equals(jon))); 
   } 
} 
  • A. true true
  • B. true false
  • C. false true
  • D. false false


C.

Note

The first assignment creates a new String "john" object.

The second line explicitly uses the new keyword, meaning a new String object is created.

Since these objects are not the same, the == test on them evaluates to false.

The equals() test on them returns true because the values they refer to are equivalent.

The correct answer is C.




PreviousNext

Related