?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Null Pointer Exception With an ArrayList of Strings

APCSA-F7GZYV

When would the following method result in a NullPointerException?

// Count the number of words that contain a lowercase Z
public static int countWordsWithZ(ArrayList<String> words)
{
    int count=0;
    for (String word : words)
    {
        if (word.indexOf("z")>=0)
            count++;
    }
    return count;
}
  I. When an array list that is not instantiated is passed as a parameter
 II. When the array list being passed as the parameter contains a null element
III. When an empty array list is passed as a parameter
 IV. When an array list containing Integer objects is passed as a parameter
A

I and II only.

B

I and III only.

C

II and III only.

D

I, II, and IV only.

E

II, III, and IV only.