?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

ArrayIndexOutOfBoundsException When Comparing Array Elements

APCSA-RJBVD6

Which of the following fragments of code will result in an ArrayIndexOutOfBoundsException error?

A
int count = 0;
int[] testScores = {78, 95, 82, 88, 100};
for(int i=0; i<testScores.length-1; i++)  
{
    if(testScores[i] > testScores[i+1])
        count++;
}
B
int count = 0;
int[] testScores = {78, 95, 82, 88, 100};
for(int i=testScores.length-1; i>0; i--)  
{
    if(testScores[i] > testScores[i-1])
    count++;
}
C
int count = 0;
int[] testScores = {78, 95, 82, 88, 100};
for(int i=0; i<testScores.length; i++)
{
    if(i != testScores.length-1 && 
        testScores[i] > testScores[i+1])
    count++;
}
D
int count = 0;
int[] testScores = {78, 95, 82, 88, 100};
for(int i=0; i>=testScores.length; i++)
{
    if(testScores[i]> testScores[i+1])
        count++;
}
E
int count = 0;
int[] testScores = {78, 95, 82, 88, 100};
for(int i=testScores.length-1; i>=0; i--)  
{
    if(testScores[i]> testScores[i+1])
        count++;
}