What is the output if this class is run with java Main cars carts?
public class Main { public static void main(String... books) { StringBuilder sb = new StringBuilder(); for (String book : books) sb.insert(sb.indexOf("c"), book); System.out.println(sb); } }
A. cars
B. cars carts
C. ccars arts
D. The code does not compile.
E. The code compiles but throws an exception at runtime.
E.
The first time through the loop, we are calling indexOf on an empty StringBuilder.
This returns -1. Since we cannot insert at index -1, the code throws an exception.