Free Version
Moderate

# Run Time Analysis of Two Methods with Array Parameter

APCSA-BXVVPY

Consider the following two methods.

// return the last number in the array that is greater than 10
// return -1 if no numbers > 10 are found
public static int methodA(int[] nums)
{
int index = -1;
for (int i=0; i<nums.length; i++)
{
if (nums[i] > 10)
index = i;
}
if (index >=0)
return nums[index];
return -1;
}

// return the last number in the array that is greater than 10
// return -1 if no numbers > 10 are found
public static int methodB(int[] nums)
{
int index = -1;
for (int i=nums.length-1; i>=0; i--)
{
if (nums[i] > 10)
return nums[i];
}
return -1;
}


When would methodA execute faster than methodB?

A

When the last number in nums that is greater than 10 is one of the last elements in the array.

B

When the last number in nums that is greater than 10 is one of the first elements in the array.

C

When there are no numbers greater than 10 in the nums.

D

methodA is always faster than methodB.

E

methodA is never faster than methodB.