AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Printing an ArrayList Created Within While Loop

APCSA-6MERTL

Consider the following code segment.

ArrayList<String> list = new ArrayList<String>();
String phrase = "We love AP Computer Science";
int space = phrase.indexOf(" ");

while (space>=0)
{
    list.add(phrase.substring(0,space-1));
    phrase = phrase.substring(space+1);
    space = phrase.indexOf(" ");
}
System.out.println(list);

What is printed as a result of executing the code segment?

A
[W, lov, A, Compute, Scienc]
B
[We, love, AP, Computer]
C
[W, lov, A, Compute]
D
[We, love, AP, Computer, Science]
E
"We love AP Computer Science"