AP CSA Unit 4 Day 26: Arraylist Loop Adding
Share
Unit 4, Data Collections • Cycle 2
Day 26 Advanced Practice • Harder Difficulty
Focus: ArrayList Loop Adding
Hard
ArrayList Loop Adding
Advanced Practice Question
Format: ArrayList Loop Adding
What is the final size of the ArrayList?
What is the final size of the ArrayList?
ArrayList list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
for (int i = 0; i < 3; i++) {
list.add(list.get(i) * 2);
}
System.out.println(list.size());
Difficulty: Hard |
Topic: ArrayList Loop Adding |
Cycle: 2 (Advanced)
Why This Answer?
Initial size is 3. Loop runs exactly 3 times (i = 0, 1, 2) based on the condition i < 3. Each iteration adds one element. Final size: 3 + 3 = 6.
Common Mistake
Watch Out!
Thinking the loop will run based on list.size() which changes during the loop (it doesn't - loop condition is i < 3).
AP Exam Strategy
Loop condition i < 3 is evaluated once at the start of each iteration. It doesn't dynamically change with list.size().
Master This Topic
This Cycle 2 HARD question tests arraylist loop adding. Review Unit 4 concepts to build mastery of data collections.
- Understanding arraylist loop adding
- Tracing code execution accurately
- Avoiding common pitfalls
Ready for More Challenges?
Cycle 2 questions prepare you for the hardest AP CSA exam questions.
Study Games Practice FRQs