Limited access

Upgrade to access all content for this subject

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.

Select an assignment template