?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

2-Dimensional Array For Each Loop of Shirt Objects

APCSA-NVEDEK

Consider the following class.

public class Shirt 
{
    public Shirt(String style, int numButtons)
    {
        // code not shown
    }

    /**
     * @return number of buttons on the shirt
     */
    public int getNumButtons()
    {
        // code not shown
    }
}

Which of the following methods will return the total number of buttons on all shirts in the two-dimensional array passed as the parameter to the method?

A
public static int totalButtons(Shirt[][] shirts)
{
    int total = 0;
    for (Shirt garments : shirts)
        for (Shirt g : garments)
            total += g.getNumButtons();
    return total;
}
B
public static int totalButtons(Shirt[][] shirts)
{
    int total = 0;
    for (Shirt[][] garments : shirts)
    {
        for (Shirt[] g : garments)
        {
            total += g.getNumButtons();
        }
    }
    return total;
}
C
public static int totalButtons(Shirt[][] shirts)
{
    int total = 0;
    for (Shirt[] garments : shirts)
    {
        for (Shirt g : garments)
        {
            total += g.getNumButtons();
        }
    }
    return total;
}
D
public static int totalButtons(Shirt[][] shirts)
{
    int total = 0;
    for (Shirt[] garments : shirts)
    {
        for (Shirt[] g : garments)
        {
            total += g.getNumButtons();
        }
    }
    return total;
}
E
public static int totalButtons(Shirt[][] shirts)
{
    int total = 0;
    for (Shirt garments : shirts)
    {
        for (Shirt[] g : garments)
        {
            total += g.getNumButtons();
        }
    }
    return total;
}