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








Question

Which of the following are output by this code? (Choose all that apply)

     3: String s = "ABC"; 
     4: String t = new String(s); 
     5: if ("ABC".equals(s)) System.out.println("A"); 
     6: if (t == s) System.out.println("B"); 
     7: if (t.equals(s)) System.out.println("C"); 
     8: if ("ABC" == s) System.out.println("D"); 
     9: if ("ABC" == t) System.out.println("E"); 
  1. A
  2. B
  3. C
  4. D
  5. E
  6. The code does not compile.




Answer



A, C, D.

Note

The code compiles fine.

Line 3 points to the String in the string pool.

Line 4 calls the String constructor explicitly and creates a new reference for s.

Lines 5 and 7 check for object value equality.

Line 6 uses object reference equality.

Finally, line 8 compares one object from the string pool with one that was explicitly constructed and returns false.