AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Debugging Code: Two Arrays

APCSA-KD4LET

Consider the following code segment.

// 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.

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

Which of the following BEST describes why she did not see the bonus points printed each time after executing the loop?

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.