Java OCA OCP Practice Question 776

Question

What is the output of the following application?

package mypkg3; //w  w  w  .j a  v a 2  s  . c  o  m
public class Main { 
   public static void main(String[] edges) { 
      final String ceiling = "up"; 
      String floor = new String("up"); 
      final String wall = new String(floor); 
      System.out.print((ceiling==wall)  
         +" "+(floor==wall)  
         +" "+ceiling.equals(wall)); 
   } 
} 
  • A. false false false
  • B. true true true
  • C. false true true
  • D. false false true
  • E. It does not compile.


D.

Note

The code compiles without issue, so Option E is incorrect.

The key here is that none of the variables are assigned the same object due to the use of the new keyword.

Comparing any two variables with == will always result in an evaluation of false, making the first two values of the print statement be false and false.

They all have an underlying String value equivalent to up, so calling equals() on any two variables will return true.

Option D is the correct answer that matches what the application will print.




PreviousNext

Related