AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Method Equivalence With Multiple If Statements

APCSA-4DYYG1

Consider the following method.

public static int getSubType(int mainType)
{
    int subType = 0;

    while(true)
    {
        if(mainType == 7)
        {
            subType = 4;
            break;
        }
        if(mainType == 9)
        {
            subType = 6;
            break;
        }
        if(mainType == 11)
        {
            subType = 9;
            break;
        }
        break;
    }
    return subType;
}

Which of the following methods are equivalent to this method?
I.

public static int getSubType(int mainType)
{
    if(mainType == 7)
        return 4;
    if(mainType == 9)
        return 6;
    if(mainType == 11)
        return 9;
    return 0;
}

II.

public static int getSubType(int mainType)
{
    int subType = 0;

    while(mainType==7 || mainType==9 || mainType==11)
    {
        if(mainType == 11)
        {
            subtype = mainType-2;
        }
        else
            subtype = mainType-3;
    }
    return subType;
}

III.

public static int getSubType(int mainType)
{
    if(mainType==7 || mainType==9 || mainType == 11)
    {
        return mainType – 3 + mainType/10;
    }
    return 0;
}
A

I only.

B

II only.

C

I and II only.

D

I and III only.

E

I, II, and III.