?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

ArrayList of Items and Prices

APCSA-FLHNV6

Consider the following method.

public static ArrayList createArrayList(ArrayList<String> items, ArrayList<Double> prices)
{
    ArrayList list = new ArrayList();
    for (int i=0; i<prices.size(); i++)
    {
        if (prices.get(i)>0)
        {
            list.add(items.get(i));
            list.add(prices.get(i));
        }
        else
        if (items.get(i).substring(0,1).equals("r"))
        {
            list.add(prices.get(i));
        }
        else
        {
            list.add(items.get(i) + "has no price");
        }
    }
    return list;
}

What will be contained in list after the following segment of code from the same class is executed?

ArrayList<String> items = new ArrayList<String>();
items.add("gravy");
items.add("turkey");
items.add("dressing");
items.add("rolls");
ArrayList<Double> prices = new ArrayList<Double>();
prices.add(0.0);
prices.add(15.0);
prices.add(10.50);
prices.add(0.0);
ArrayList list = createArrayList(items, prices);
A

An error occurs when adding different data types to list.

B

[gravy has no price, turkey, 15.0, dressing, 10.5, 0.0]

C

[gravy, 0.0, turkey, 15.0, dressing, 10.5, rolls, 0.0]

D

[gravy has no price, 0.0, turkey, 15.0, dressing, 10.5, rolls has no price, 0.0]

E

[gravy has no price, turkey, 15.0, dressing, 10.5, rolls has no price]