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








Question

What is the result of the following code?

     int total = 0; 
     StringBuilder letters = new StringBuilder("abcdefg"); 
     total += letters.substring(1, 2).length(); 
     total += letters.substring(6, 6).length(); 
     total += letters.substring(6, 5).length(); 
     System.out.println(total); 

  1. 1
  2. 2
  3. 3
  4. 7
  5. An exception is thrown.
  6. The code does not compile.




Answer



E.

Note

letters.substring(6, 5) indexes in reverse order and causes a StringIndexOutOfBoundsException.