?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Searching for Last Word in String Array

APCSA-NVETYW

Given an array of words and also a search word as its two parameters, which of the following methods will return the position of the last word in the array that exactly matches the search word?

I.

public static int findLastWord(String[] words, String search)
{
    for (int i=words.length-1; i>=0; i--)
    {
        if (words[i].equals(search))
            return i;
    }
    return -1;
}

II.

public static int findLastWord(String[] words, String search)
{
    int i=0;
    for (String w : words)
    {
        if (w.equals(search))
            return i;
        i++;
    }
    return -1;
}

III.

public static int findLastWord(String[] words, String search)
{
    int n=-1;
    for (int i=0; i<words.length; i++)
    {
        if (words[i].equals(search)) 
            n=i;
    }
    return n;
}
A

I only.

B

II only.

C

III only.

D

I and III only.

E

I and II only.