AP Computer Science A · Topic notes
Data Collections
Key ideas, common mistakes, and a worked example for Data Collections in AP Computer Science A.
17 untimed questions from the free practice test.
Exam weighting: 30–40%
Data Collections is the largest unit, at 30–40% of the multiple-choice section. It covers arrays, ArrayList, 2D arrays, reading data from text files, wrapper classes, searching, sorting, recursion, and the ethics of collecting data. Free-response questions 3 and 4 both come from this unit.
Key ideas
- Array indices run from 0 to
length - 1. A new array is filled with default values: 0, 0.0,false, ornull. - An enhanced
forloop gives a copy of each element, so assigning to the loop variable does not change an array of primitives. - Removing from an
ArrayListshifts later elements left. When removing inside an index loop, loop backward or do not increase the index after a removal. - For a 2D array,
arr.lengthis the number of rows andarr[0].lengthis the number of columns. Put the column loop on the outside to traverse in column-major order. - Binary search needs sorted data and halves the search range on each step. You trace recursive methods on the exam, but you are not asked to write them.
Common mistakes
- Mixing up
length,length(), andsize(). Arrays uselength, strings uselength(), and anArrayListusessize(). - Starting a maximum at 0, which fails when every value is negative.
- Mixing up rows and columns, such as using
arr.lengthas the bound for the column index. - Assuming a data set is fair because it is large. Data collected from only some people can be biased.
Worked example
Consider the following method. What does countAbove return when arr is {{4, 9, 2}, {7, 1, 8}} and limit is 5?
public static int countAbove(int[][] arr, int limit)
{
int count = 0;
for (int[] row : arr)
{
for (int val : row)
{
if (val > limit)
{
count++;
}
}
}
return count;
}Solution
The outer enhanced for loop takes each row in turn, and the inner one takes each value in that row. The values greater than 5 are 9, 7, and 8, so the method returns 3.
Keep going
More AP Computer Science A topics
Stuck on Data Collections?
A tutor can find the gap in one session. Book a free consultation.
Every question on this site is original and written by Bright Sprouts Academy. AP, Advanced Placement, SAT, and PSAT/NMSQT are registered trademarks of College Board, which is not affiliated with and does not endorse this site. PSAT/NMSQT is also a trademark jointly owned by College Board and National Merit Scholarship Corporation.
Last updated . Exam board and university requirements change, so always check the official specification and admissions pages.