AP® Computer Science A

Free Version

Upgrade subject to access all content


Debugging Code: Two Arrays


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++)

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++)

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


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


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


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


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


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