AP® Computer Science A Data Collections Multiple Choice Questions
Work through AP Computer Science A multiple-choice questions on Data Collections, 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.
Subtopic: Arraylists Clear filter
Question 152
Question 153
Question 154
Question 155
Question 156
Question 157
Question 158
Question 159
Question 160
String whose length is less than 3 from list.
public static void cleanup(ArrayList<String> list)
{
for (int i = 0; i < list.size(); i++)
{
if (list.get(i).length() < 3)
{
list.remove(i);
}
}
}
Given the initial list ["a", "bb", "ccc", "dd", "eeee", "ff", "gg"], what does list contain after a call to cleanup(list)?Main Solution
Solution:
Correct: D. Classic index-skipping bug: after removing, i still increments, skipping the element that shifts into position i. Trace: i=0 "a" remove → [bb,ccc,dd,eeee,ff,gg]; i=1 "ccc" keep; i=2 "dd" remove → [bb,ccc,eeee,ff,gg]; i=3 "ff" remove → [bb,ccc,eeee,gg]; i=4, size=4 so loop ends. Final: ["bb","ccc","eeee","gg"].
Answer: Correct: D
Answer images
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.

