AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Creating a New Array Based on Values from Another Array

APCSA-XEEUWY

The waist sizes of a sample of overweight adult males were taken and recorded in an array named waistSizes. You are asked to create a method that would return an array of ideal waist sizes which are 10% below their original waist sizes.

The original and ideal waist sizes should be printed with the following segment of code:

int[] waistSizes = {45, 38, 34, 40, 46, 35, 39};
int[] idealWaistSizes = calcIdealWaistSizes(waistSizes);
for (int i=0; i<waistSizes.length; i++)
{
    System.out.println("before diet: " + waistSizes[i] + ",   after diet: " +idealWaistSizes[i]);
}

Which of the following methods would correctly return the ideal waist sizes?

I.
public static int[] calcIdealWaistSizes(int[] origSizes)
{
    int[] idealWaistSizes = new int[origSizes.length];
    for (int i=0; i<origSizes.length; i++)
    {
        idealWaistSizes[i] = (int) (origSizes[i]*.9);
    }
    return idealWaistSizes;
}
II.
public.static int[] calcIdealWaistSizes (int[] origSizes)
{
    int[] idealWaistSizes = origSizes;
    for (int i=0; i<origSizes.length; i++)
    {
        idealWaistSizes[i] = (int) (idealWaistSizes[i]*.9);
    }
    return idealWaistSizes;
}
III.
public static int[] calcIdealWaistSizes(int[] origSizes)
{
    int[] idealWaistSizes = new int[origSizes.length];
    int i=0;
    for (int size : origSizes)
    {
        idealWaistSizes[i] = size * 0.9;
        i++;
    }
    return idealWaistSizes;
}
A

I, II, and III

B

I and II only

C

III only

D

II only

E

I only