AP® Computer Science A Multiple Choice Questions
Work through AP Computer Science A multiple-choice questions unit by unit, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Multiple choice
Unit 4: Data Collections
Question 2792
Multiple choice
Unit 4: Data Collections
Question 2793
Multiple choice
Unit 4: Data Collections
Consider the following code segment.
ArrayList<Integer> values = new ArrayList<Integer>();
values.add(2);
values.add(5);
values.add(1);
values.add(5);
for (int i = values.size() - 1; i >= 0; i--)
{
if (values.get(i) == 5)
values.remove(i);
}
System.out.println(values);
What is printed?
Pick an answer, then check it. The solution opens in a window.
DCorrect answer
Main Solution
Answer
Answer: D. The loop moves backward through the list, removing both occurrences of 5 without skipping any elements. The remaining values are 2 and 1.
Answer: Correct: D
A
[2, 5, 1]
B
[2, 1, 5]
C
[2]
D
[2, 1]
Answer images
Question 2794
Multiple choice
Unit 4: Data Collections
Question 2795
Multiple choice
Unit 4: Data Collections
Question 2796
Multiple choice
Unit 4: Data Collections
Question 2797
Multiple choice
Unit 4: Data Collections
Question 2798
Multiple choice
Unit 4: Data Collections
Question 2799
Multiple choice
Unit 4: Data Collections
Question 2800
Multiple choice
Unit 4: Data Collections
Question 2,791 of 2,811
Page 280 of 282
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

