?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Looping through an ArrayList of Objects

APCSA-GJZ6EY

Given the following method:

public static Object listString(ArrayList list)
{
    int count=0;
    for (int i=list.size()-1; i>=0; i--)
    {
        if (list.get(i) instanceof String)
        {
            if (((String) list.get(i)).length()<10)
            {
                if (count==1) 
                    return list.get(i);
                count++
            }
        }
   {
   return null;
}

And given the following code from the main method:

ArrayList list = // a list of Objects
Object o = listString(list);

Which best describes what listString will return if it reaches the return statement in the loop?

A

The last String in the list that is less than 10 characters long

B

The count of the number of Strings in the list

C

The first String in the list that is less than 10 characters long

D

The next-to-last String in the list that is less than 10 characters long

E

The second String in the list that is less than 10 characters long