?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Calculate Golf Scores Over Par from Integer Arrays

APCSA-NEGYT6

Given an array of the par for each golf hole, and an equally sized array of scores that a golfer makes on each corresponding hole:

int[] pars =   {4, 4, 3, 4, 5, 4, 3, 5, 4, 4, 4, 3, 4, 5, 4, 3, 5, 4};
int[] scores = {5, 4, 2, 6, 4, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

Which of the following methods will return the number of strokes over or under par that the golfer is during his round (i.e. using the above example, it would return 4)?

Note: if a score on a hole is 0, then the golfer has not yet played that hole and it should not be considered in the calculation. Also, a golfer does not necessarily start his/her round on the first hole (it is common to start a round on hole 10).

I.

public static int diffFromPar(int[] pars, int[] scores)
{
    int diff=0;
    for (int i=0; i<pars.length; i++)
    {
        if (scores[i]==0)  
            break;
        diff += scores[i] - pars[i];
    }
    return diff;
}

II.

public static int  diffFromPar(int[] pars, int[] scores)
{                     
    int totPars=0, totScores=0;
    for (int i=0; i<pars.length; i++)
    {
        if (scores[i]!=0)
        {
            totPars+=pars[i];
            totsScores+=scores[i];
        }
    }
    return totScores - totPars;
}

III.

public static int diffFromPar (int[] pars, int[] scores)
{
    int totPars=0, totScores=0;
    for (int i=0; i<pars.length; i++)
    {
        totPars+=pars[i];
        totScores+=scores[i];
    }
    return totScores - totPars;
}
A

I only.

B

II only.

C

III only.

D

I and II only.

E

II and III only.