Java OCA OCP Practice Question 289

Question

Given:

public class Main {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder(8);
    System.out.print(sb.length() + " " + sb + " ");
    sb.insert(0, "abcdef");
    sb.append("789");
    System.out.println(sb.length() + " " + sb);
  }
}

What is the result?

A.  0 8 abcdef78
B.  0 8 789abcde
C.  0 9 abcdef789
D.  0 9 789abcdef
E.  Compilations fails
F.  0, followed by an exception


P:C is correct.

Note

The append() method adds value to the end of the StringBuilder's current value.

If you append past the current capacity, the capacity is automatically increased.

Invoking insert() past the current capacity will cause an exception to be thrown.




PreviousNext

Related