OCA Java SE 8 Core Java APIs - OCA Mock Question Core Java APIs 2-1








Question

What is the result of the following code?


public class Main{
   public static void main(String[] argv){
       String s1 = "java"; 
       StringBuilder s2 = new StringBuilder("java"); 
       if (s1 == s2)               //line A
        System.out.print("1"); 
       if (s1.equals(s2)) 
        System.out.print("2"); 
   }
}
     
  1. The code does not compile.
  2. No output is printed.
  3. An exception is thrown.
  4. 1
  5. 2
  6. 12




Answer



A.

Note

Java does not allow you to compare String and StringBuilder using ==. The compiler will tell you that the incompatible operand type.