?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Inserting Letters from Word into ArrayList of Strings

APCSA-EFYJIV

Consider the following code segment.

ArrayList<String> letters = new ArrayList<String>();
String word = "widget";

for (int i=0; i<word.length(); i++)
{
    int idx=1;
    if (i%2==1)  
        idx--;
    letters.add(idx,word.substring(i,i+1));
}
System.out.println(letters);

What will the following segment of code print to the console?

A

Throws an error.

B
[i, w, g, d, t, e]
C
[w, i, d, g, e, t]
D
[w, d, i, e, g, t]
E
[t, e, g, d, I, w]