Skip to main content

AP Computer Science A Unit 4: Data Collections

Ready When You Are

Select a course and unit, then start practicing.

Data Collections typically accounts for about 30%–40% of the AP Computer Science A multiple-choice section. That makes it worth dedicated practice even if your class spent only a few weeks on it.

Using and implementing common data structures and algorithms to store and process collections of data. On the AP Computer Science A exam, items from Data Collections often ask you to apply course concepts to a scenario you have not memorized verbatim rather than recall isolated terms.

College Board topic statements for this unit include Arrays and array processing algorithms; ArrayList and other collection usage patterns; Searching and sorting basics (selection, insertion, simple sorts); and Traversing and manipulating collections; iteration patterns.

On the AP Computer Science A exam, Data Collections is weighted at roughly 30%–40% of the multiple-choice section. That is enough to matter on score day even if your class spent only a few weeks here—especially when questions combine this unit with later material.

A common miss on Data Collections is choosing the definition you recognize instead of the one that fits the scenario. Watch for stems that swap closely related ideas from Arrays and array processing algorithms; and ArrayList and other collection usage patterns.

Alternate timed sets with untimed review. Speed matters on exam day, but accuracy during study builds the foundation.

When Data Collections feels easy, increase difficulty by explaining why each wrong answer tempts you. That metacognition is what separates recognition from exam-ready mastery.

Click Practice above for fresh MCQs tied to Data Collections. Answer, read the explanation, and log one sentence about why the correct choice works—that habit compounds faster than grinding endless worksheets.

Sample questions

A few exam-style multiple-choice questions from this unit, with answers and explanations.

  1. Question 1

    Consider the following method, which searches an ArrayList from the beginning and returns an index when it finds a match.

    int findIndex(ArrayList<Integer> data, int target)
    {
        for (int i = 0; i < data.size(); i++)
        {
            if (data.get(i) == target)
            {
                return i;
            }
        }
        return -1;
    }

    What value is returned by findIndex when data contains [18, 7, 12, 7, 5, 12] and target is 12?

    • A

      -1

    • B

      0

    • C

      2

      Correct answer
    • D

      5

    Explanation

    The method examines elements in increasing index order. It first finds the target value 12 at index 2 and immediately returns that index.

    B: Index 0 contains 18, not the target.

    C: Correct. Index 2 is the first occurrence of 12.

    D: Index 5 is another occurrence, but the method returns as soon as it finds the earlier one.

    A: The method returns -1 only if it finishes the traversal without finding the target.

  2. Question 2

    Consider the following Java code, which performs exactly one pass through the array:

    int[] values = {7, 2, 6, 3, 5};
    for (int j = 0; j < values.length - 1; j++) {
        if (values[j] > values[j + 1]) {
            int temp = values[j];
            values[j] = values[j + 1];
            values[j + 1] = temp;
        }
    }

    What are the contents of values after the loop finishes?

    • A

      {2, 6, 3, 7, 5}

    • B

      {2, 6, 3, 5, 7}

      Correct answer
    • C

      {2, 3, 5, 6, 7}

    • D

      {7, 2, 6, 3, 5}

    Explanation

    The loop compares each adjacent pair from left to right and swaps the pair when it is out of ascending order. The value 7 moves right as it is compared with each following value: after the comparisons, the array is {2, 6, 3, 5, 7}.

    B is correct because one full pass moves the largest value to the final position, but does not necessarily sort the entire array.

    C is incorrect because it assumes that one pass completely sorts the array.

    D is incorrect because it assumes no swaps occur, even though the first adjacent pair is out of order.

    A is incorrect because it reflects the array before the final adjacent comparison, which swaps 7 and 5.

  3. Question 3

    A program stores sensor readings in an ArrayList<Integer>. The list is not empty. The method below returns an index in the list.

    public static int indexOfLargest(ArrayList<Integer> readings)
    {
        int bestIndex = 0;
        for (int i = 1; i < readings.size(); i++)
        {
            if (readings.get(i) > readings.get(bestIndex))
            {
                bestIndex = i;
            }
        }
        return bestIndex;
    }

    What value is returned by indexOfLargest when readings contains [17, 23, 31, 31, 26, 31]?

    • A

      5

    • B

      3

    • C

      2

      Correct answer
    • D

      31

    Explanation

    The method starts with index 0 as the current largest. As it traverses the list, it updates bestIndex when it finds a strictly larger value. The first largest value, 31, is at index 2. Later values equal to 31 do not satisfy the strict > comparison, so the method returns 2.

    C is correct because index 2 is the first index containing the largest value. B is incorrect because index 3 contains an equal value, but the method does not update the index when values are equal. A is incorrect because index 5 also contains an equal value and is not selected. D is incorrect because the method returns an index, not the largest reading itself.

  4. Question 4

    Consider the following code:

    ArrayList<Integer> first = new ArrayList<Integer>();
    first.add(4);
    first.add(7);
    first.add(2);
    
    ArrayList<Integer> alias = first;
    ArrayList<Integer> copy = new ArrayList<Integer>();
    for (int value : first)
    {
        copy.add(value);
    }
    
    alias.set(1, alias.get(1) + 5);
    copy.add(first.get(0));
    first.set(2, copy.get(1));

    What are the contents of alias and copy, in that order, after the code executes?

    • A

      alias: [4, 7, 7]; copy: [4, 7, 2, 4]

    • B

      alias: [4, 12, 7]; copy: [4, 7, 2, 4]

      Correct answer
    • C

      alias: [4, 12, 7]; copy: [4, 12, 7, 4]

    • D

      alias: [4, 12, 2, 4]; copy: [4, 7, 2, 4]

    Explanation

    alias and first refer to the same ArrayList, so alias.set(1, 12) changes first as well. The loop adds each value to a separate list, so copy is independent and initially contains [4, 7, 2]. Then copy.add(first.get(0)) makes copy [4, 7, 2, 4]. Finally, first.set(2, copy.get(1)) sets index 2 of both first and alias to 7.

    B: Correct. It accounts for the shared reference between first and alias and the independent contents of copy.

    A: Incorrect. It treats alias as a separate list, so it misses the change made through alias at index 1.

    D: Incorrect. It treats adding an element to copy as if it also added that element to the shared first and alias list.

    C: Incorrect. It assumes changes to first also update copy, but the loop created a separate list by adding the values one at a time.

  5. Question 5

    A data file contains the integer tokens shown below, separated by whitespace:

    8 3 11 6 3 10

    The following code reads the file, keeps values of at least 6 in an ArrayList, and then processes entries at even list indices. Assume the file is opened successfully. What value is printed?

    Scanner input = new Scanner(new File("scores.txt"));
    ArrayList<Integer> scores = new ArrayList<Integer>();
    
    while (input.hasNextInt())
    {
        int score = input.nextInt();
        if (score >= 6)
        {
            scores.add(score);
        }
    }
    
    int total = 0;
    for (int i = 0; i < scores.size(); i++)
    {
        if (i % 2 == 0)
        {
            total += scores.get(i);
        }
    }
    System.out.println(total);
    • A

      14

      Correct answer
    • B

      22

    • C

      35

    • D

      21

    Explanation

    The file-reading loop adds only values of at least 6, so scores becomes [8, 11, 6, 10]. The traversal adds the values at indices 0 and 2, which are 8 and 6, for a total of 14.

    C is incorrect because 35 is the sum of every value in the filtered list, not just values at even indices.

    B is incorrect because 22 is the sum of values at even indices in the original file, before filtering.

    D is incorrect because 21 is the sum of the filtered list's values at odd indices.

Frequently asked questions