?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Run Time Analysis of Two Methods

APCSA-AVQEVX

Consider the following two methods.

public static ArrayList<String> changeMaybe1(ArrayList<String> attend)
{
    for (int i=0; i <attend.size(); i++)
    {
        if (attend.get(i).equals("maybe"))
        {     
            attend.remove(i);
            attend.add(i,"no");
        }
    }
    return attend;
}

public static ArrayList<String> changeMaybe2(ArrayList<String> attend)
{
    for (int i=0; i<attend.size(); i++)
    {
        if (attend.get(i).equals("maybe"))
            attend.set(i, "no");
    }
    return attend;
}

Which of the following most accurately describes the comparison between these two methods?

A

changeMaybe1 will generally run faster than changeMaybe2. Both methods will provide the same result.

B

changeMaybe1 will generally run faster than changeMaybe2. The methods will provide different results.

C

changeMaybe2 will generally run faster than changeMaybe1. Both methods will provide the same result.

D

changeMaybe2 will generally run faster than changeMaybe1. The methods will provide different results.

E

changeMaybe1 will have the same speed as changeMaybe2. Both methods will provide the same result.