?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Sequential Search Algorithm Missing Code

APCSA-Y7GEPE

The following method performs a sequential search on an ArrayList of Strings. If word is found the method returns the index and if word is not found the method returns -1.

public static int seqSearch(ArrayList<String> list, String word)
{
    /*missing code*/
}

Which of the following should replace /*missing code*/?

A
for (int i = 0; i < list.size(); i++) 
{
    if (list[i].equals(word))
        return i;
}
return -1;
B
for (int i = 0; i < list.size(); i++) 
{
    if (word == list.get(i))
        return i;
}
return -1;
C
for (int i = 0; i < list.size(); i++) 
{
    if (word.equals(list.get(i)))
        return i;
    else
        return -1;
}
D
int loc = -1;
for (int i = list.size()-1; i > -1; i--) 
{
    if (list.get(i).equals(word)) 
    {
        loc = i;
        return loc;
    }
}
return loc;
E
for (int i = list.size()-1; i > 0; i--) 
{
    if (word.equals(list.get(i)))
        return i;
}
return -1;