?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Index Out Of Bounds on an ArrayList of Strings

APCSA-6QNGJZ

Given that petList is an ArrayList of Strings that contains {"dog", "snake", "snake", "cat"},
which of the following fragments of code will result in an IndexOutOfBoundsException?

I.

for(int i=0; i<petList.size(); i++)
{
    if(petList.get(i).equals("snake"))   
        petList.remove(i);
}

II.

for(int i=0; i<4; i++)
{
    if(petList.get(i).equals("snake")
    {
        petList.remove(i);
        i--;
    }
}

III.

for(int i=0; i<petList.size(); i++)
{
    if (petList.get(i).equals("snake"))
    {
        petList.remove(i);
        i--;
    }
}
A

I only.

B

II only.

C

III only.

D

I and II only.

E

I and III only.