?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Equivalent Methods That Process an ArrayList

APCSA-GXFYVD

Consider the following method written by a programmer.

public static String getLetter(ArrayList<String> letters)
{
    for (int i=0; i <letters.size();i++)
    {    
        if (i==letters.size()-1)
            return letters.get(i);
    }
    return "?";
}

Which of the following methods are equivalent to this method, but are more efficient?
I.

public static String getLetter (ArrayList<String> letters)
{
    String letter = letters.get(i);
    for (String e : letters)
    {
        letter = letters.get(i);
    }
    return letter;            
}

II.

public static String getLetter(ArrayList<String> letters)
{
    if ( letters.get(0) instanceof String )
        return letters.get(letters.size());   
    return “?”;
}

III.

public static String getLetter(ArrayList<String> letters)
{
    if ( letters.size()==0 )
        return “?”;
    else
        return letters.get(letters.size()-1);            
}
A

II only.

B

III only.

C

I and II only.

D

I and III only.

E

II and III only.