Limited access

Upgrade to access all content for this subject

Which of the following methods will always return the index of the first element that equals the designated tree type or -1 if the tree type is not found? Note: The parameters for each method are the array of tree types and the tree type to search for.

I.

public static int findFirstTree(String[] trees, String treeType)
{
    for (int i=0; i<trees.length-1; i++)
        if (trees[i].equals(treeType))
            return -1;
}

II.

public static int findFirstTree(String[] trees, String treeType)
{
    int i=0;
    for (String tree : trees)
    {
        if (tree.equals(treeType))
            return i;
        i+=1;
    }
    return -1;
}

III.

public static int findFirstTree(String[] trees, String treeType)
{
    int i=0;
    for ( ; i<trees.length && !trees[i].equals(treeType); i++)  ;
    if (i==trees.length)
        return -1;
    return i;
}
A

I only.

B

I and II only.

C

I and III only.

D

II and III only.

E

I, II, and III.

Select an assignment template