?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

ArrayList String Count

APCSA-1IFEV6

ArrayList a is an array list without a type parameter, so it can contain any type of Object.

public static int countStrings(ArrayList a)
{
    int count=0;
    for (int i=0; i<a.size(); i++)
    {
        /* insert if statement here */
    }
    return count;
 }

Which of the following if statements should replace /*insert if statement here */ such that the following method will correctly count the number of Strings that are greater than 3 characters long in ArrayList a?

A
if ( ((String) a.get(i)).length() > 3 && a.get(i) instanceof String)
    count++;
B
if ((String) a.get(i)).length() > 3)
    count++;
C
if (a.get(i) instanceof String && ((String) a.get(i)).length() > 3)
    count++;
D
if (a.get(i) instanceof String && a.get(i).length() > 3)
    count++;
E
if (!(a.get(i) instanceof String) || a.get(i).length() > 3)
    count++;