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








Question

What is the result of the following code? (Choose all that apply)

public class Main{
   public static void main(String[] argv){
     StringBuilder numbers = new StringBuilder("0123456789"); 
     numbers.delete(2,  8); 
     numbers.append("X").insert(2, "+"); 
     System.out.println(numbers);    
   }
}
  1. 01+89X
  2. 012+9X
  3. 012+X9
  4. 0123456789
  5. An exception is thrown.
  6. The code does not compile.




Answer



A.

Note

First, the code deletes the characters from index 2 until the character before index 8. At this point, 0189 is in numbers StringBuilder.

Then it appends a dash to the end of the characters sequence, resulting in 0189X, and then inserts a plus sign at index 2, resulting in 01+89X.