?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Furniture ArrayList of Strings

APCSA-E4PT11

Consider the following code segment.

for (int i=0; i<furniture.size(); i++)
{
    furniture.add(i, furniture.get(furniture.size()-i-1));
    if (furniture.get(i).compareTo("d")>0)
    {
        furniture.remove(i);
        i--;
    }
}

If furniture is an ArrayList of Strings which contains [”sofa”, “couch”, “tv”, “desk”, “lamp”, “table”] before the loop begins, what is the result after the segment of code is executed?

A

furniture contains [”sofa”, “couch”, “tv”, “desk”, “lamp”, “table”]

B

furniture is an empty array list (size = 0)

C

furniture contains [”couch”, “desk”]

D

furniture contains [”desk”, “couch”]

E

the code gets into an infinite loop until there is a StackOverflow error