AP Computer Science Exam Practice 10
AP Computer Science Exam Practice 10
System.out.println();
}
What is printed as a result of executing this code segment?
(A) A E I
F J
K
(B) B F J
C G K
D H L
(C) E I
F J
G K
H L
(D) F G H
J K L
(E) F J
G K
H L
int i = 0;
while (i < numList.size())
{
int num = numList.get(i);
if (num % key == 0)
{
numList.remove(i);
returnList.add(num);
}
i++;
}
return returnList;
}
As an example, if the method is called with an ArrayList containing the values [5, 2, 10, 20, 16]
and the parameter key has the value 5, then numList should contain [2, 16] at the end of the
method and an ArrayList containing [5, 10, 20] should be returned.
Which of the following best explains why the method does not always work as intended?
(A) The method attempts to add an element to returnList after that element has already been removed
from numList.
(B) The method causes a NullPointerException to be thrown when no matches are found.
(C) The method causes an IndexOutOfBoundsException to be thrown.
(D) The method fails to correctly determine whether an element of numList is divisible by key.
(E) The method skips some elements of numList during the traversal.