AP Computer Science A, AP exams, 30–40%
Unit 4: Data Collections
Unit 4 carries the largest published weighting in the course. It covers arrays, two-dimensional arrays, and ArrayList, along with the traversal patterns, standard algorithms, and searching and sorting that operate on them. Text files and data sets were added in the revised framework, extending the unit toward working with real data.
Unit 4: Data Collections at a glance
- Position
- 4 of 4 units
- Published weighting
- 30–40% in the official framework
- Topics covered
- 6 topics in this guide
What is covered
Topics inside this unit.
These topics describe the scope students most often review for this part of AP Computer Science A. Treat them as a study checklist rather than an exhaustive list of what a single exam form can sample.
- Arrays and traversal
- Declaring and initializing arrays, indexing, the length field, and standard and enhanced for traversals.
- Array algorithms
- Finding a maximum or minimum, summing, counting matches, and shifting or reversing elements.
- Two-dimensional arrays
- Row-major structure, nested traversal, and algorithms that work across rows and columns.
- ArrayList
- Creating and modifying an ArrayList, its methods, and why removing during traversal requires care.
- Searching and sorting
- Linear and binary search, selection and insertion sort, and the requirements and costs of each.
- Text files and data sets
- Reading structured data into a collection and processing it with the algorithms above.
Study tips
How to practice Data Collections.
Section-specific guidance that follows from how this material is usually tested.
- Practice the standard array and ArrayList algorithms until you can write them from memory without a template.
- Work removal-during-traversal problems deliberately; the index shift is a classic exam trap.
- Give this unit the most practice time; its published weighting is the largest of the four.
Where this fits
Unit 4: Data Collections is 4 of 4 units in the AP Computer Science A guide, and it carries a published weighting of 30–40% in the official framework. Use the full guide to balance it against everything else you have to review.
Sample Data Collections questions
Practice questions, not yet expert-reviewedOriginal practice questions written to the published framework for this section. No expert has reviewed them, and they are not taken from any official exam. Try each one before you open the answer.
- Question 1Foundational
A Java array is declared as int[] a = new int[8]. What is the index of its last element?
- A7
- B8
- C0
- D-1
Show answer
Answer: A. 7
Array indexes start at 0, so an array of length 8 uses indexes 0 through 7 and a.length is 8. Using index 8 throws an ArrayIndexOutOfBoundsException, which is the classic off-by-one error in traversals.
Key insight: Valid indexes run from 0 to length - 1, so length itself is never a valid index.
- Question 2Foundational
Which Java expression gives the number of elements currently in an ArrayList named list?
- Alist.size()
- Blist.length
- Clist.length()
- Dlist.count()
Show answer
Answer: A. list.size()
ArrayList exposes the method size(), while length is the field used by arrays and length() is the method used by String. Mixing the three is one of the most common compile errors on the free-response section.
Key insight: Arrays use length, Strings use length(), and ArrayList uses size().
- Question 3Intermediate
A Java program declares int[][] grid = {{1, 2, 3}, {4, 5, 6}}. What is the value of grid[1][2]?
- A6
- B3
- C5
- DAn exception is thrown
Show answer
Answer: A. 6
Two-dimensional arrays are row-major, so grid[1] is the second row {4, 5, 6} and index 2 within that row is 6. Reading the indexes as column then row gives 3, which is grid[0][2].
Key insight: In Java the first index selects the row and the second selects the column within it.
- Question 4Intermediate
A loop traverses an ArrayList with an index i that increases by 1 on every pass and calls list.remove(i) whenever an element matches a target. What problem does this cause?
- AElements shift left after a removal, so the next element is skipped
- BThe program always throws an exception on the first removal
- CThe list is silently sorted as a side effect
- DNothing; the loop removes every matching element correctly
Show answer
Answer: A. Elements shift left after a removal, so the next element is skipped
Removing index i moves every later element one position toward the front, so the element that slides into position i is never examined once i is incremented. The fix is to leave i unchanged after a removal, or to traverse the list from the last index down to 0.
Key insight: After removing at index i, do not increment i; the next element already occupies that position.
- Question 5Advanced
Selection sort is used to sort the array {5, 2, 9, 1} into increasing order. What does the array contain after the first pass?
- A{1, 2, 9, 5}
- B{2, 5, 1, 9}
- C{1, 2, 5, 9}
- D{5, 2, 9, 1}
Show answer
Answer: A. {1, 2, 9, 5}
Selection sort scans the whole array for the smallest value, 1 at index 3, and swaps it with the element at index 0, giving {1, 2, 9, 5}. The choice {2, 5, 1, 9} is the result of a first pass of bubble sort, which only swaps adjacent elements.
Key insight: Selection sort places one final value per pass with a single swap; bubble sort swaps neighbors repeatedly.
Continue through AP Computer Science A
Practice Data Collections deliberately.
Create your Arcade Academy workspace to turn this topic list into spaced-repetition reviews, linked notes, and progress you can act on.