?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Easy

Index Out of Bounds Error With an ArrayList of Strings

APCSA-FXPT4E

Consider the following method.

// Count the number of words that contain "and";
public static int countWordsWithAnd(ArrayList<String> words)
{
    int count=0;
    for (int i=words.size()-1; i>=0; i--)
    {
        if (words.get(i).indexOf("and")>=0)
            count++;
    }
    return count;
}

When would the method result in an index out of bounds error?

A

When "and" is not found in any of the array list elements.

B

When the array list being passed as the parameter contains an empty string.

C

When an empty array list is passed as a parameter.

D

An out of bounds error occurs every time this method is executed.

E

The method will never throw an out of bounds error.