Limited access

Upgrade to access all content for this subject

Consider the following method written by a programmer. The method returns true if both name1 and name2 are elements in names. Otherwise it returns false.

public static boolean containsNames(ArrayList<String> names, String name1, String name2)
{
    boolean foundName1 = false;
    boolean foundName2 = false;
    for (String n : names)
    {
        if (n.equals(name1))
        {
            foundName1 = true;
        }
        else
        if (n.equals(name2))
        {
            foundName2 = true;
        }
    }
    return foundName1 && foundName2;
}

For example, calling the method as follows will produce this result:

ArrayList<String> names = 
    new ArrayList<String>(Arrays.asList("Sam","Greg","Sally","Van","Kathy"));

System.out.println(containsNames(names,"Greg", "Kathy"));  //prints true
System.out.println(containsNames(names,"Van", "Kelly"));   //print false

The programmer is given an additional requirement that the method needs to work for a variable number of search names, not only two search names. Therefore, the programmer decides to create an array list to store the search names. Which of the following methods would accept the array list of names and the array list of searchNames, and return true if all of the search names are found in names?

A
public static boolean containsNames(ArrayList<String> names, ArrayList<String> searchNames)
{
    boolean foundName = false;
    for (String search : searchNames)
    {
        for(String name : names)
        {
            if (search.equals(name))
            {    
                foundName = true;
                break;
            }    
        }
        if (!foundName)
            return false;
    }
    return true;
}
B
public static boolean containsNames(ArrayList<String> names, ArrayList<String> searchNames)
{
    boolean foundName = false;
    for (String search : searchNames)
    {
        for(String name : names)
        {
            if (search.equals(name))
                foundName = true;
            else
                foundName = false;
        }
        return foundName;
    }
    return true;
}
C
public static boolean containsNames(ArrayList<String> names, ArrayList<String> searchNames)
{
    for (String search : searchNames)
    {
        boolean foundName = false;
        for(String name : names)
        {
            if (search.equals(name))
            {    
                foundName = true;
                break;
            }    
        }
        if (!foundName)
            return false;
    }
    return true;
}
D
public static boolean containsNames(ArrayList<String> names, ArrayList<String> searchNames)
{
    for (String name : names)
    {
        boolean foundName = false;
        for(String search : searchNames)
        {
            if (search.equals(name))
            {    
                foundName = true;
                break;
            }    
        }
        if (!foundName)
            return false;
    }
    return true;
}
E
public static boolean containsNames(ArrayList<String> names, ArrayList<String> searchNames)
{
    boolean foundName = false;
    for (String name : names)
    {
        for(String search : searchNames)
        {
            if (search.equals(name))
            {    
                foundName = true;
                break;
            }    
        }
        if (!foundName)
            return false;
    }
    return true;
}
Select an assignment template