SAT / PSAT
SAT / PSAT Prep
History & Social Science
AP World History AP US History AP European History AP Human Geography AP US Government & Politics AP Psychology AP Macroeconomics AP Microeconomics
English
AP English Language & Composition AP English Literature & Composition
Math & Computer Science
AP Calculus AB/BC AP Precalculus AP Statistics AP Computer Science A AP Computer Science Principles
Sciences
AP Biology AP Chemistry AP Environmental Science AP Physics 1 AP Physics 2
World Languages & Arts
AP Spanish Language AP Art History AP Music Theory Start studying →
Unit 4 · Data Collections Flashcards Cheat Sheet Essentials Visual Review MC Practice FRQ Practice

AP Computer Science A Unit 4 FRQ Practice

Practice a College Board-style Data Analysis free response on arrays and ArrayList — averaging, counting, and safe removal. Write your Java, then reveal the model answer to see exactly what earns each point.

← Back to Unit 4 hub
Free Response Question · Unit 4 · Data Collections

You are given an int[] scores (an array of test scores) and an ArrayList<Integer> nums. Write methods that process these collections. This mirrors the exam’s Data Analysis question.

A
Write a method average(int[] scores) that returns the average of the values as a double.

✓ Model answer

public double average(int[] scores) {
  int sum = 0;
  for (int s : scores) {
    sum += s;
  }
  return (double) sum / scores.length;
}

Why it scores: Traverses every element, accumulates a sum, and divides by scores.length using a double cast for a real average. Using integer division (sum / scores.length) would truncate and lose the point.
B
Write a method countAbove(int[] scores, int cutoff) that returns how many scores are strictly greater than cutoff.

✓ Model answer

public int countAbove(int[] scores, int cutoff) {
  int count = 0;
  for (int s : scores) {
    if (s > cutoff) {
      count++;
    }
  }
  return count;
}

Why it scores: Traverses the array, uses a strict > comparison against cutoff, increments a counter inside the if, and returns it. Using >= (not strict) would miscount.
C
Write a method removeEvens(ArrayList<Integer> nums) that removes every even value from nums. Explain how you avoid skipping elements.

✓ Model answer

public void removeEvens(ArrayList<Integer> nums) {
  int i = 0;
  while (i < nums.size()) {
    if (nums.get(i) % 2 == 0) {
      nums.remove(i);
    } else {
      i++;
    }
  }
}

When an element is removed, later elements shift left, so I do not increment i after a removal — the next element has moved into index i. I only advance i when I keep an element.

Why it scores: Correctly removes even values and avoids the skip bug by not advancing the index after a removal (or equivalently looping backwards). Blindly using a for loop with i++ after remove would skip elements and lose credit.

How to score points on AP CSA free response