AP CSA Unit 4 Day 16: Arraylist Set Index Error

Unit 4, Data Collections • Cycle 2
Day 16 Advanced Practice • Harder Difficulty
Focus: ArrayList Set Index Error Hard ArrayList Set Index Error

Advanced Practice Question

Format: ArrayList Set Index Error

What happens when this code executes?
ArrayList words = new ArrayList<>();
words.add("cat");
words.add("dog");
words.add("bird");

words.set(5, "fish");

System.out.println(words);
Difficulty: Hard  |  Topic: ArrayList Set Index Error  |  Cycle: 2 (Advanced)
Why This Answer?

set(index, value) replaces an existing element. Index 5 is out of bounds (valid indices are 0-2). Throws IndexOutOfBoundsException at runtime.

Common Mistake
Watch Out!

Thinking set() automatically extends the ArrayList like add() does.

AP Exam Strategy

set() can only replace existing elements. Use add() to extend the list.

Master This Topic

This Cycle 2 HARD question tests arraylist set index error. Review Unit 4 concepts to build mastery of data collections.

  • Understanding arraylist set index error
  • Tracing code execution accurately
  • Avoiding common pitfalls
View Unit 4 Study Guide

Ready for More Challenges?

Cycle 2 questions prepare you for the hardest AP CSA exam questions.

Study Games Practice FRQs
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.