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








Question

What is the result of the following code?

     3: String s = "ABCD"; 
     4: s.toUpperCase(); 
     5: s.trim(); 
     6: s.substring(1, 3); 
     7: s += " two"; 
     8: System.out.println(s.length()); 
  1. 2
  2. 4
  3. 8
  4. 10
  5. An exception is thrown.
  6. The code does not compile.




Answer



C.

Note

String objects are immutable.

Line 4 5 6 have no impact on the s value.

public class Main{
   public static void main(String[] argv){
     String s = "ABCD"; 
     s.toUpperCase(); /*from w  ww  .j av  a2s.c o  m*/
     s.trim(); 
     s.substring(1, 3); 
     s += " two"; 
     System.out.println(s.length()); 
      
   }
}