AP Computer Science A, Unit 4
Exam words: Data Collections
The 51 words a written answer in this unit needs. For each one: what it means, a wording that loses the mark and why, and a sentence that uses it the way a full answer would.
Study these words as a deck
Flashcards, Learn, Test and Match in Cards, with your Still learning marks kept, and the booklet to print on A4 or US Letter. Sign in to open it.
The words
data set
A collection of related values or records gathered to answer questions or support decisions.
Loses the mark: “collecting every field is always better”. Each extra field costs storage and raises the harm if the data leaks; collect what the question needs.
Example: The Brookfield bike-share data set holds one record per ride, start, end and minutes, which helps place new docks and also reveals riders' daily routes.
data privacy
People's control over how data about them is collected, stored, used and shared.
Loses the mark: “removing names makes data anonymous”. Other fields together, such as a birth date, a ZIP code and a route, can still point to one person.
Example: Storing only the hour of each ride, not exact GPS tracks, protects data privacy in the Brookfield app while still showing which docks are busy.
personally identifiable information
Data that can identify a specific person, alone or combined with other data.
Loses the mark: “only a name counts as personally identifiable information”. An email, a student number or a home address also identifies a person.
Example: Riders' emails and home addresses are personally identifiable information, so the Brookfield app keeps them out of the data set it shares with the city.
bias in data
A data set or method that leaves out or misrepresents some groups, skewing its results.
Loses the mark: “a program's results are objective whatever the data”. A program computes from the data it gets, so gaps in the data carry straight into its answers.
Example: If the Brookfield survey reaches only app users, its results have bias in data: residents without smartphones are left out of the dock plan.
data structure
A way of organizing a program's data, such as an array or a list.
Loses the mark: “an array grows as new records arrive”. An array's length is fixed when it is made; a list that grows calls for an ArrayList.
Example: The Brookfield app chooses its data structure by the job: an `ArrayList<Ride>` that grows each day, not a fixed-length array.
array
A fixed-length, numbered row of values of one type, stored under one name.
Loses the mark: “assigning one array variable to another copies the array”. `tue = mon;` copies the reference, so both names refer to one array.
Example: `int[] laps = new int[5];` makes an array of five lap times in the Swiftwater swim app, each starting at 0.
index
An element's position number in an array, list or string, counting from 0.
Loses the mark: “indexes start at 1”. The first element is at index 0, so `laps[1]` is the second lap.
Example: In the Swiftwater app, `laps[0]` is the first lap and `laps[4]` the fifth, the last index of the 5-element array.
element
One value stored in an array or list, reached by its index.
Loses the mark: “a position and the value there are the same answer”. The index says where; the element is what is stored there.
Example: After `laps[2] = 61;`, the element at index 2 of the Swiftwater array is 61.
array length
The number of elements an array holds, read as arr.length with no parentheses.
Loses the mark: “length counts only the values stored”. `length` is the size the array was made with, whatever has been assigned.
Example: The array length of `new int[5]` is 5 in the Swiftwater app, even before any lap is recorded, so `laps.length` is 5.
ArrayIndexOutOfBoundsException
The exception thrown for an array index below 0 or at least the length.
Loses the mark: “reading past the end gives a default value”. Java checks every index and throws an exception; it never returns a default.
Example: Reading `temps[3]` from a 3-element array throws an `ArrayIndexOutOfBoundsException` in the Coldspring weather app.
initializer list
Values listed in braces that create and fill an array in one step.
Loses the mark: “an initializer list makes an array one longer than its values”. The array's length is exactly the number of values in the braces.
Example: `int[] stock = {3, 8, 1};` uses an initializer list, so the Pantry app's array has length 3 and `stock[2]` is 1.
swap
Exchanging two values, using a temporary variable so neither is lost.
Loses the mark: “two assignments swap two elements”. `a[i] = a[j]; a[j] = a[i];` copies one value over the other and loses the first; a temporary variable is needed.
Example: To swap `a[i]` and `a[j]` in the Tumbler sorting app: `int temp = a[i]; a[i] = a[j]; a[j] = temp;`.
shift
Moving each element one position along an array, overwriting as it goes.
Loses the mark: “shifting right works with a forward loop”. A forward loop copies the first value into every later cell; shifting right must run from the end back.
Example: To shift the Ferris queue right, the loop runs `for (int i = q.length - 1; i > 0; i--) { q[i] = q[i - 1]; }`, from the back so no value is overwritten first.
File
The class whose object names a file on disk, for a Scanner to read.
Loses the mark: “a Scanner built on a file's name reads the file”. `new Scanner("plants.txt")` reads the text "plants.txt" itself; it needs `new Scanner(new File("plants.txt"))`.
Example: The Greenhouse log opens its data with `Scanner in = new Scanner(new File("plants.txt"));`, a File object naming the file to read.
Scanner
The class that reads a file or other input piece by piece or by line.
Loses the mark: “nextLine after nextInt reads the next line”. `nextInt` leaves the rest of its line, so the first `nextLine` returns that leftover, often an empty string.
Example: The Greenhouse log's Scanner reads one plant per line with `in.nextLine()` while `in.hasNextLine()` is true.
text file
A file of lines of characters that a program can read as strings.
Loses the mark: “the pieces of a line are numbers”. Text read from a file is a String; "12" must be parsed before it can be added.
Example: The text file `plants.txt` for the Greenhouse log holds one line per plant, such as `fern,12,40`.
IOException
The checked exception for a failed read or write, such as a missing file.
Loses the mark: “an IOException is thrown every time a file is read”. It is thrown only when reading fails, such as when the file cannot be found.
Example: If `plants.txt` is missing, opening it throws an `IOException`, so the Greenhouse log's `readPlants` method must declare it.
throws clause
The part of a method header that declares the checked exceptions it may throw.
Loses the mark: “throws IOException is only a note”. A method that reads a file does not compile without the throws clause or a catch.
Example: The header `public static int countPlants(String name) throws IOException` gives the Greenhouse log's method its throws clause, so it compiles.
checked exception
An exception the compiler makes a method catch or declare, such as IOException.
Loses the mark: “every exception must be declared with throws”. Only checked exceptions must be; `ArithmeticException` and `NullPointerException` need no declaration.
Example: `IOException` is a checked exception, so the Greenhouse log's `readPlants` must declare it, while its division by zero needs no declaration.
split
The String method that breaks a line into an array of pieces at each separator.
Loses the mark: “split removes the spaces around each piece”. `"12, 40".split(",")` gives "12" and " 40", with the space kept.
Example: In the Greenhouse log, `String[] parts = line.split(",");` turns `fern,12,40` into three Strings, so `parts[1]` is "12".
Integer.parseInt
The method that turns digit text such as "12" into the int 12.
Loses the mark: “parts[1] + parts[2] adds the two numbers”. Both pieces are Strings, so + joins them; each must go through `Integer.parseInt` first.
Example: The Greenhouse log adds water with `Integer.parseInt(parts[1]) + Integer.parseInt(parts[2])`, so 12 and 40 give 52, not "1240".
end of file
The point where a file has no more data; hasNext() or hasNextLine() then gives false.
Loses the mark: “a Scanner loop stops by itself at the end of the file”. The loop must test `hasNextLine()`; calling `nextLine()` past the end throws an exception.
Example: The Greenhouse log loops `while (in.hasNextLine())`, so it stops at the end of file instead of reading past it.
ArrayList
A list class that grows and shrinks as elements are added or removed.
Loses the mark: “removing an element leaves a gap”. `remove` shifts every later element left by one, so no gap remains.
Example: After `colors.remove(1)` on the Paintbox app's ArrayList `["red", "plum", "fig"]`, it is `["red", "fig"]` and `fig` is at index 1.
wrapper class
A class such as Integer or Double that holds one primitive value as an object.
Loses the mark: “a list can be declared with int”. `ArrayList<int>` does not compile; an ArrayList holds objects, so it is `ArrayList<Integer>`.
Example: The Tempest weather app stores readings in an `ArrayList<Double>`, using the wrapper class `Double`, because `ArrayList<double>` does not compile.
autoboxing
Java turning a primitive into its wrapper object automatically, as when adding to a list.
Loses the mark: “a wrapper list cannot take a plain number”. `temps.add(18.5)` compiles: autoboxing turns the `double` into a `Double`.
Example: `temps.add(18.5);` works on an `ArrayList<Double>` in the Tempest app through autoboxing: the `double` becomes a `Double` object.
unboxing
Java turning a wrapper object back into its primitive value automatically.
Loses the mark: “a value taken from an ArrayList<Integer> cannot be used in arithmetic”. Unboxing turns the `Integer` into an `int`, so `total += list.get(i);` compiles.
Example: In the Tempest app, `double t = temps.get(0);` uses unboxing to turn the stored `Double` into a `double`.
generic type
A class with a type parameter, like ArrayList<E>, given its element type in angle brackets.
Loses the mark: “remove with a number removes that value”. On an `ArrayList<Integer>`, `remove(1)` removes the element at index 1, not the value 1.
Example: `ArrayList<String>` gives the generic type its element type, so the Paintbox app's `colors.add(7)` does not compile.
enhanced for loop
A loop visiting each element of an array or list in turn, without an index.
Loses the mark: “an enhanced for loop can add to or remove from its list”. Changing a list's size inside its own enhanced for loop throws a `ConcurrentModificationException`.
Example: The Ledger app totals prices with the enhanced for loop `for (double p : prices) { total += p; }`, and removes items in an indexed loop instead.
IndexOutOfBoundsException
The exception thrown when an ArrayList position is below 0 or at least size().
Loses the mark: “get(size()) returns the last element”. Positions run from 0 to `size() - 1`, so `get(size())` throws an `IndexOutOfBoundsException`.
Example: The Tagline app saved `n = tags.size()` before removing tags, so `tags.get(i)` with `i` at the old size throws an `IndexOutOfBoundsException`.
two-dimensional array
An array of arrays that stores values in rows and columns, reached as grid[row][col].
Loses the mark: “the column index comes first”. The row comes first: `heights[1][2]` is row 1, column 2.
Example: The Avalon theater app's `boolean[][] sold = new boolean[8][12];` is a two-dimensional array of 8 rows and 12 seats a row.
array of arrays
How Java builds a 2D array: an array whose elements are row arrays.
Loses the mark: “a row taken from a grid is a separate copy”. `int[] shelf = stock[1];` holds a reference to row 1, so changing `shelf[0]` changes the grid.
Example: Because a 2D array is an array of arrays, `int[] shelf = stock[1];` in the Barrow pantry app refers to row 1 itself.
row-major order
Visiting a grid one row at a time, left to right along each row.
Loses the mark: “swapping the loops changes nothing”. Putting the column loop outside visits the same cells in column-major order, so the output order changes.
Example: Printing the Avalon seat map in row-major order uses `r` in the outer loop and `c` in the inner loop, finishing row 0 before row 1.
column-major order
Visiting a grid one column at a time, top to bottom down each column.
Loses the mark: “column-major order means the column index is written first”. The access is still `grid[r][c]`; only the loops swap, with `c` outside.
Example: The Avalon app totals each seat column in column-major order, with `c` in the outer loop and `r` in the inner loop.
row
One horizontal line of a 2D array; grid[r] is row r, itself an array.
Loses the mark: “one running total gives each row its own sum”. The total must restart at 0 before each row, inside the outer loop.
Example: The Avalon app resets `total = 0` at the start of each row, so it prints one sum for every row.
column
One vertical line of a 2D array: grid[r][c] for every row r and one c.
Loses the mark: “a column has grid[0].length cells”. A column has one cell per row, so it has `grid.length` cells.
Example: To total column 3 of the Avalon grid, the loop runs `r` from 0 to `sold.length - 1` and reads `sold[r][3]`.
linear search
Checking elements one by one from the start until finding the target or running out.
Loses the mark: “a linear search always checks every element”. It stops at the first match, so a target near the front takes only a few checks.
Example: A linear search for 16 in the Oakhurst ticket list `{4, 9, 16, 2}` checks 4, 9 and 16, then returns index 2.
binary search
Searching sorted data by checking the middle and discarding the half without the target.
Loses the mark: “binary search works on any array”. It relies on order; on unsorted data it can skip right past the target.
Example: A binary search of 1,000 sorted prices in the Corner Market app needs at most about 10 checks, because each check halves what is left.
sorted
In order, smallest to largest or by another rule, as binary search requires.
Loses the mark: “the next range can keep mid”. The middle was already checked, so the next range starts at `mid + 1` or ends at `mid - 1`.
Example: The Corner Market prices must be sorted before a binary search, or the search can discard the half that holds the target.
selection sort
A sort that finds the smallest remaining value each pass and swaps it into place.
Loses the mark: “selection sort swaps whenever it sees a smaller value”. Each pass only tracks where the smallest is, then makes one swap at the end of the pass.
Example: After the first pass of selection sort on `{15, 9, 4, 22}`, the Pebble app's array is `{4, 9, 15, 22}`: 4 was swapped with 15.
insertion sort
A sort that inserts each next value in place by shifting larger values right.
Loses the mark: “insertion sort's front holds the smallest values”. The front is sorted among the values seen so far, but a smaller value may still come later.
Example: On its third pass over `{9, 15, 22, 4}`, insertion sort in the Pebble app shifts 22, 15 and 9 right to insert 4, giving `{4, 9, 15, 22}`.
sorting algorithm
An algorithm that puts the elements of an array or list in order.
Loses the mark: “a sort costs the same on any data”. Insertion sort on nearly sorted data shifts almost nothing; on reversed data it shifts the most.
Example: For the nearly sorted Pebble lists, insertion sort is the better sorting algorithm: it makes few shifts, while selection sort still scans every pass.
in-place sort
A sort that rearranges the array itself instead of building a new one.
Loses the mark: “a void sort method leaves the caller's array alone”. The method receives a reference to the caller's array, so an in-place sort changes it.
Example: The Pebble app's `selectionSort(int[] a)` is an in-place sort: it returns nothing, yet the caller's array is sorted afterward.
recursion
Solving a problem by having a method call itself on a smaller version of it.
Loses the mark: “all the calls share one n”. Each call has its own parameter `n`, so values come back as each call finishes.
Example: Using recursion, the Echo Valley app's `sum(n)` returns `n + sum(n - 1)` until `n` is 1.
base case
The case a recursive method answers directly, without calling itself, so the calls stop.
Loses the mark: “the base case's value is the answer”. The base case's value is only the start; each waiting call adds its part on the way back.
Example: In `sum(4)` from the Echo Valley app, the base case `if (n == 1) { return 1; }` stops the calls, and the answer builds back up to 10.
recursive call
A call a method makes to itself, on an input closer to the base case.
Loses the mark: “code after a recursive call never runs”. It runs when that call returns, in reverse order of the calls.
Example: In the Echo Valley app's `echo(n)`, the recursive call `echo(n - 1)` comes before the print, so `echo(3)` prints 1, then 2, then 3.
infinite recursion
Recursion that never reaches a base case, so calls go on until memory runs out.
Loses the mark: “a recursion stops by itself near 0”. Nothing stops it: without a base case that is reached, calls continue until a `StackOverflowError`.
Example: `countdown(n)` in the Rocket Club app calls `countdown(n - 2)` and stops only at `n == 0`, so `countdown(5)` is infinite recursion.
StackOverflowError
The error Java throws when too many method calls are waiting, as in infinite recursion.
Loses the mark: “a StackOverflowError means a number got too big”. It means the call stack ran out of room, almost always from recursion that never stops.
Example: `countdown(5)` in the Rocket Club app skips past 0 and ends with a `StackOverflowError` after thousands of waiting calls.
call stack
The list of method calls still running, newest on top, each with its own variables.
Loses the mark: “two recursive calls make one call per value of n”. Each call that makes two calls doubles the work, so the call count grows much faster than n.
Example: While `sum(3)` runs in the Echo Valley app, the call stack holds `sum(3)`, `sum(2)` and `sum(1)`, each with its own `n`.
merge sort
A sort that splits the array in half, sorts each half recursively, then merges them.
Loses the mark: “splitting the array sorts it”. Splitting only makes smaller pieces; the merges put the values in order.
Example: Merge sort in the Quarry app splits `{8, 3, 5, 1}` into `{8, 3}` and `{5, 1}`, sorts them to `{3, 8}` and `{1, 5}`, then merges to `{1, 3, 5, 8}`.
recursive binary search
Binary search as a method calling itself on the half that could hold the target.
Loses the mark: “recursive binary search searches both halves”. It calls itself on one half only; searching both halves is what merge sort's sorting does.
Example: The Corner Market's recursive binary search calls itself on `lo` to `mid - 1` when the target is less than the middle price.
divide and conquer
Splitting a problem into smaller parts, solving each, and combining the results.
Loses the mark: “merge sort follows one half, like binary search”. Merge sort sorts both halves; binary search is the one that follows only one.
Example: Merge sort is divide and conquer: the Quarry app splits 8 values down to single values, then combines them in sorted pairs, fours and eights.