AP Computer Science A, Unit 2
Exam words: Selection and Iteration
The 37 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
boolean
Java's primitive type with exactly two values, true and false.
Loses the mark: “a comparison can only appear inside an if”. A comparison is a boolean value like any other: it can be stored, printed or returned.
Example: The Ironwood arcade app stores `boolean out = lives == 0;`, so `out` holds the boolean false while `lives` is 3.
boolean expression
An expression that evaluates to true or false, such as a comparison.
Loses the mark: “a boolean expression must contain a comparison”. A boolean variable on its own, such as `member`, is already a boolean expression.
Example: `age >= 13` is a boolean expression in the Maplewood library app: for a 15-year-old it evaluates to true.
relational operator
An operator that compares two values and gives a boolean: <, <=, >, >=, == or !=.
Loses the mark: “> and >= give the same answer”. They differ when the values are equal: `70 >= 70` is true, but `70 > 70` is false.
Example: The relational operator in `score >= 70` makes the Kinsley quiz app pass a score of exactly 70.
equality operator
== or !=; on primitives it compares values, on references whether both name one object.
Loses the mark: “== tells whether two objects hold the same values”. On references, `==` is true only when both variables refer to the same object; equal contents need `equals`.
Example: With the equality operator, `scanned == pass` in the Fenwick ticket app is false for two separate String objects holding "VIP", while `scanned.equals(pass)` is true.
if statement
A statement that runs its block only when its boolean condition is true.
Loses the mark: “an if condition must contain a comparison”. Any boolean works: `if (member)` is a complete condition.
Example: The if statement `if (temp > 30) { ... }` in the Dunmore weather app prints a heat warning only when the temperature is above 30.
if-else statement
An if statement with an else block, which runs exactly one of its two blocks.
Loses the mark: “the else block runs after the if block”. Exactly one block runs: the if block when the condition is true, otherwise the else block.
Example: In the Hollow Creek pool app, the if-else statement prints "shorts" when `temp > 25` and "coat" otherwise, never both.
else-if chain
Linked if, else if and else blocks where only the first true block runs.
Loses the mark: “every true branch of an else-if chain runs”. The chain stops at the first true condition, so later true conditions are never tested.
Example: In the Marlow grading app's else-if chain, a score of 92 meets `score >= 80` first and gets a B, because the `score >= 90` test comes later.
nested if
An if statement placed inside another if statement's block.
Loses the mark: “code after a chain belongs to its last branch”. A statement after the closing brace is outside every branch and runs whichever branch ran.
Example: With a nested if, the Pinegrove cinema app checks `age < 18` only inside the `member` block, so non-members never reach the age test.
branch
One of the paths of code a decision can take; only the chosen one runs.
Loses the mark: “both branches run and the last one wins”. Only one branch of an if-else runs; the other is skipped.
Example: For a temperature of 18, the Fairhaven app takes the else branch and prints "jacket"; the if branch is skipped.
logical operator
An operator that combines or reverses boolean values: && (and), || (or) and ! (not).
Loses the mark: “an and in the rule always means && in the code”. "Children and seniors" means a person who is either, so the code needs `age < 12 || age >= 65`.
Example: The Brookhaven museum app uses logical operators in `age < 12 || age >= 65` to give the discount to children or seniors.
AND operator
&&: true only when both sides are true.
Loses the mark: “&& is true when either side is true”. `&&` needs both sides true; one true side is enough only for `||`.
Example: The AND operator in `hasTicket && age >= 18` lets a Fieldstone concert visitor in only when both conditions are true.
OR operator
||: true when at least one side is true, including when both are.
Loses the mark: “|| means one or the other but not both”. `||` is also true when both sides are true.
Example: With the OR operator, `subscribed || rented` in the Reelhouse film app is true for a viewer who has done both.
NOT operator
!: reverses a boolean, turning true into false and false into true.
Loses the mark: “a not in front of parentheses applies to the first part only”. `!(a && b)` reverses the whole expression inside the parentheses.
Example: The NOT operator in `!raining` makes the Larch Hill picnic app plan a picnic only when `raining` is false.
short-circuit evaluation
Skipping the right side of && or || when the left side already decides the result.
Loses the mark: “both sides of && and || are always evaluated”. If the left side of `&&` is false, or of `||` is true, the right side never runs.
Example: Short-circuit evaluation keeps `count != 0 && total / count > 5` safe in the Alder school app: when `count` is 0, the division never runs.
De Morgan's laws
!(a && b) is equivalent to !a || !b, and !(a || b) to !a && !b.
Loses the mark: “a not moves inside without flipping the operator”. Moving `!` inside also swaps `&&` and `||`: `!(a && b)` is `!a || !b`, not `!a && !b`.
Example: By De Morgan's laws, the Westlake alarm app's `!(temp > 30 && raining)` can be rewritten `temp <= 30 || !raining`.
truth table
A table listing a boolean expression's value for every combination of its inputs.
Loses the mark: “two rows are enough to check an expression with two variables”. Two boolean variables have four combinations, so the table needs four rows.
Example: A four-row truth table for `a && !b` in the Galloway quiz app shows it is true only in the row where `a` is true and `b` is false.
while loop
A loop that tests its condition before each pass and repeats while it is true.
Loses the mark: “a while loop always runs its body at least once”. The condition is tested first, so a while loop whose condition starts false runs zero times.
Example: The while loop `while (balance < 1000) { balance += 200; }` in the Sterling savings app runs until a test finds `balance` at 1000 or more.
loop condition
The boolean expression tested before each pass; the loop ends once it is false.
Loses the mark: “after the loop, its variable holds the last value that passed”. The loop ends on the first value that fails the loop condition, so the variable holds that value.
Example: With `page` starting at 1, the loop condition `page <= 10` in the Bramble e-reader app is checked 11 times for 10 passes; the 11th check, at page 11, is false.
loop body
The statements inside a loop's braces, run once on each pass.
Loses the mark: “the loop body runs one extra time after the condition fails”. Once the condition is false the body is skipped and the statement after the loop runs.
Example: In the Cobblestone stamp app, the loop body `{ total += 5; stamps--; }` runs once for each stamp, and is skipped entirely when `stamps` is 0.
infinite loop
A loop whose condition never becomes false, so it never stops on its own.
Loses the mark: “a loop that can never end will stop by itself”. Nothing stops it: if the body never changes the condition's result, the loop runs forever.
Example: `while (page <= 10) { System.out.println(page); }` is an infinite loop in the Bramble app, because nothing in the body changes `page`.
off-by-one error
A loop running one pass too many or too few, often from < where <= was meant.
Loses the mark: “k < 4 and k <= 4 give the same number of passes”. Starting at 0, `k < 4` runs 4 times and `k <= 4` runs 5 times.
Example: Writing `i <= 5` for five days starting at `i = 0` causes an off-by-one error in the Larkfield step app: it adds six days.
iteration
Repeating a set of statements; one iteration is one pass through a loop body.
Loses the mark: “the pass count equals the loop's last value”. Count the passes from the start value to the stop: `k` from 0 while `k < 4` makes 4 passes, not 3.
Example: Starting at 1 and doubling while below 100, the Petri Lane bacteria model makes 7 iterations: 1, 2, 4, 8, 16, 32 and 64.
for loop
A loop whose header holds the initialization, the condition and the update.
Loses the mark: “the update runs before the first pass”. The update runs after each pass; the first pass uses the starting value.
Example: The for loop `for (int i = 3; i <= 12; i += 3)` in the Rosewood band app adds 3, 6, 9 and 12, with the update running after each pass.
loop control variable
The variable a loop's condition tests and its update changes, such as i.
Loses the mark: “only the header controls a for loop”. A statement in the body that changes the loop control variable also changes the passes.
Example: In `for (int day = 1; day <= 7; day++)`, the loop control variable `day` takes 1 through 7 in the Hartfield diary app.
counting loop
A loop that runs a number of times known before it starts, using a counter.
Loses the mark: “the loop runs once for every value up to the bound”. The step decides which values are visited: from 3 to 12 by 3 gives only 3, 6, 9 and 12.
Example: `for (int i = 0; i < 8; i++)` is a counting loop in the Elmira seating app: it makes exactly 8 passes, one for each row.
running total
A variable set to 0 before a loop, adding each value on every pass.
Loses the mark: “a running total can be started inside the loop”. Setting the total to 0 inside the loop wipes it out on every pass, so it ends holding only the last value.
Example: The Ashgrove bake sale app keeps a running total: `int total = 0;` before the loop and `total += price;` inside it.
counter
A variable that adds 1 each time something happens, to count how often it happens.
Loses the mark: “a count adds the values it counts”. A counter adds 1 for each match; adding the values themselves makes a sum.
Example: The Brightmoor library app uses a counter, `if (days > 14) { late++; }`, to count overdue books rather than add up their days.
average
The sum over the count, computed as a double so the fraction is kept.
Loses the mark: “total / count gives the exact average”. With two `int` values that is integer division; cast one to `double` first.
Example: The Silverton swim app's average is `(double) total / count`, so 21 seconds over 6 laps gives 3.5, not 3.
maximum
The largest value, found by keeping the best so far and replacing it when beaten.
Loses the mark: “starting a maximum at 0 works for any values”. If every value is negative, 0 is never replaced; start with the first value instead.
Example: The Frostburg weather app finds the maximum by starting `best` at the first reading and replacing it with any larger one, so readings of -5, -2 and -9 give -2.
traversal
Visiting every element of a string, array or list in order, one at a time.
Loses the mark: “a string's last position equals its length”. A traversal stops at `length() - 1`; position `length()` does not exist.
Example: The traversal `for (int i = 0; i < word.length(); i++)` in the Quill spelling app visits each position from 0 to `word.length() - 1`.
empty string
The string "" with no characters, length 0, often a new string's starting value.
Loses the mark: “a String with no value starts as the empty string”. A String variable set to `null` refers to no object; only `""` is the empty string.
Example: The Mirror word app starts `String result = "";`, the empty string, then adds each letter to the front to build the reversed word.
StringIndexOutOfBoundsException
The exception thrown when a String method is given a position outside the string.
Loses the mark: “a substring that runs past the end is cut short”. Java does not shorten it: `"K9X".substring(2, 4)` throws a `StringIndexOutOfBoundsException`.
Example: In the Tallis badge app, `code.substring(3, 5)` on the 4-character `"K9XB"` throws a `StringIndexOutOfBoundsException`, because position 5 is past the end.
nested loop
A loop inside another loop's body; the inner loop runs fully on each outer pass.
Loses the mark: “the inner body runs outer plus inner times”. The counts multiply: 4 outer passes of a 5-pass inner loop run the inner body 20 times.
Example: In the Grantham seating app, a nested loop with 4 rows outside and 5 seats inside prints 20 seat labels.
inner loop
The loop inside a nested loop; the outer loop contains it and changes more slowly.
Loses the mark: “the outer loop's variable changes fastest”. The inner loop's variable runs through all its values before the outer variable changes once.
Example: In the Grantham app, the inner loop's `seat` runs 1 to 5 before the outer loop's `row` moves from 1 to 2.
statement execution count
How many times a statement runs for a given input, used to compare algorithms.
Loses the mark: “a program's work is the number of lines it has”. A one-line statement inside a loop can run thousands of times; count runs, not lines.
Example: For `n = 10`, the statement execution count of `total += i;` inside a loop over `i` from 0 to `n - 1` is 10, and of the loop test is 11.
run time
How long an algorithm takes, judged by how its work grows with the input size.
Loses the mark: “doubling the input doubles every loop's count”. A nested loop over n does four times the work when n doubles, and a halving loop adds only one pass.
Example: The run time of the Fenmore photo app's nested loop grows with `n * n`, so 200 photos take about four times the work of 100.
efficiency
How little time or memory an algorithm uses for its job, compared with others.
Loses the mark: “the shorter program is always the more efficient one”. Efficiency is the work done when it runs; a short loop inside a loop can do far more work than longer code.
Example: For 1,000 names, the Westvale app's single loop is more efficient than its nested loop: about 1,000 checks instead of about 1,000,000.