?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Debugging Code: Two Arrays

APCSA-KD4LET

Consider the following segment of code:

// scoresOriginal = raw test scores
// scoresFinal = final test scores with bonus points added to each score
int[] scoresOriginal = {90, 82, 95, 77, 65, 80};
int[] scoresFinal = scoresOriginal;
int bonusPoints = 5;

for (int i=0; i<scoresFinal.length; i++)
{
    scoresFinal[i]+=bonusPoints;
}

To test this code, the programmer used the following loop to print the difference between each corresponding element in the two arrays. She expected to see the bonus points printed each time, but this did not happen.

What is the issue?

for (int i=0; i<scoresFinal.length; i++)
{
    System.out.println(scoresFinal[i]-scoresOriginal[i]);
}
A

Some of the elements in scoresFinal are not changed as expected.

B

When the scoresFinal array is modified, the scoresOriginal array is also pointing to this same array in memory, so 0 is printed each time.

C

Each time through the loop, the bonus points are temporarily added to each original score but are not actually saved to the scoresFinal array.

D

The testing loop that prints out the differences is not coded properly.

E

The scoresFinal array is initialized to all zeros, so after the loop completes, all its elements contain the value of the bonus points.