AP CSA Unit 4: Iteration

282 multiple-choice questions on AP Computer Science A Unit 4, Iteration, organized by topic. They are not tied to any one class or teacher: they follow the College Board course framework. Learn the 37 key terms with the flashcards first, then test yourself. Every answer comes with a short explanation.

Take the practice test · Flashcards · All study tools

What it covers

  1. 4.1 while loops
  2. 4.2 for loops
  3. 4.3 String algorithms with loops
  4. 4.4 Nested loops and loop analysis

Key terms

4.1 while loops

Loop condition
The boolean expression a while or for loop checks before each iteration
Loop body
The statement or block that is executed once per iteration
Pretest loop
A loop that tests its condition before the body, so the body may run zero times
Sentinel value
A special value that marks the end of a data sequence and is not itself processed
Accumulator
A variable that is updated inside a loop to hold a running total, count, product, or other aggregate
Loop update
A statement that changes the loop control variable so the condition can eventually become false
Zero-trip loop
A loop whose condition is already false when first checked, so the body never runs
Sentinel-controlled loop
A loop that continues until a sentinel value appears in the input
while loop
Repeats its body while a condition is true
Infinite loop
A loop whose condition never becomes false

4.2 for loops

Loop header
The initialization, condition, and update written in the parentheses of a for loop
Initialization expression
The first part of a for header, which runs once before the condition is tested the first time
Update expression
The third part of a for header, which runs after each completed execution of the body
Counting loop
A loop written to run a definite number of times, most often a for loop
Decrementing loop
A loop whose control variable decreases on each iteration
Definite iteration
Repeating a body a known number of times, decided from the loop's start, bound, and step
Empty loop body
A loop whose body does nothing, often written by placing a semicolon right after the header
for loop
A loop with initialization, condition and update in its header
Off-by-one error
A loop that runs one too many or one too few times
Loop control variable
The variable that determines how many times a loop runs

4.3 String algorithms with loops

String traversal
Visiting each character of a string in index order, usually from 0 through length() - 1
Character extraction
Obtaining the character at index i with substring(i, i + 1)
String accumulator
A String variable built up by concatenation inside a loop
String reversal
Building a new string by appending characters from the last index down to 0
Palindrome
A string that is equal to its reverse
Occurrence count
The number of times a target character or substring is found during a traversal
Linear scan of a string
A single pass from one end of a string to the other that examines each character once
One-character substring
The string returned by substring(i, i + 1), which holds the character at index i

4.4 Nested loops and loop analysis

Nested loop
A loop inside another loop
Statement execution count
The number of times a statement runs for a given input
Enclosing loop
The outer loop in a nested pair, whose body contains another loop
Nested iteration
Running one loop inside another so the inner body executes once for each pair of index values
Triangular execution count
A total that arises when the inner loop's trip count depends on the outer index, often 1 + 2 + ... + n
Quadratic growth
A statement execution count that grows in proportion to the square of the input size
Trace table
A row-by-row record of variable values after each iteration, used to predict a loop's result
Independent inner bound
An inner loop whose start and end do not depend on the outer loop's control variable
Dependent inner bound
An inner loop whose range changes with the outer loop's control variable

Sample practice questions

  1. How many times does this loop print? int i = 0; while (i < 5) { System.out.print(i); i++; }

    1. 4
    2. 6
    3. Infinitely many
    4. 5

    Answer: D. 5 i takes the values 0, 1, 2, 3, 4 before the condition i < 5 fails.

  2. How many times does for (int i = 3; i < 10; i += 2) execute its body?

    1. 3
    2. 5
    3. 4
    4. 7

    Answer: C. 4 i = 3, 5, 7, 9; then 11 fails the test.

  3. What does this code print for String s = "loop"? for (int i = s.length() - 1; i >= 0; i--) System.out.print(s.substring(i, i + 1));

    1. loop
    2. pool
    3. lop
    4. An error

    Answer: B. pool It prints each character from the last to the first, reversing the string.

  4. How many times is "*" printed by this rectangular nest? for (int i = 0; i < 4; i++) for (int j = 0; j < 3; j++) System.out.print("*");

    1. 7
    2. 3
    3. 4
    4. 12

    Answer: D. 12 The inner loop runs 3 times for each of the 4 outer iterations.

  5. What is printed? int n = 1; while (n < 20) n *= 3; System.out.print(n);

    1. 9
    2. 20
    3. 81
    4. 27

    Answer: D. 27 n goes 1, 3, 9, 27; 27 < 20 is false, so the loop stops.

  6. What is printed? for (int i = 5; i > 0; i--) System.out.print(i);

    1. 54321
    2. 12345
    3. 543210
    4. 4321

    Answer: A. 54321 i counts down from 5 to 1.

  7. What does this code count? int c = 0; for (int i = 0; i < s.length(); i++) if (s.substring(i, i + 1).equals("a")) c++;

    1. The number of times "a" occurs in s
    2. The index of the first "a" in s
    3. The number of characters in s before "a"
    4. Whether s starts with the letter "a"

    Answer: A. The number of times "a" occurs in s Each one-character substring is compared to "a".

  8. How many times is "*" printed by this triangular nest? for (int i = 1; i <= 4; i++) for (int j = 1; j <= i; j++) System.out.print("*");

    1. 16
    2. 10
    3. 4
    4. 8

    Answer: B. 10 The inner loop runs 1 + 2 + 3 + 4 = 10 times in total.

  9. Which loop is infinite?

    1. int k = 10; while (k > 0) { k--; }
    2. int k = 5; while (k > 0) { System.out.print(k); }
    3. int k = 0; while (k < 10) { k += 2; }
    4. int k = 1; while (k < 100) { k *= 2; }

    Answer: B. int k = 5; while (k > 0) { System.out.print(k); } k is never changed inside the loop, so k > 0 stays true forever. The other loops move k toward ending the loop.

  10. Which for loop is equivalent to int i = 0; while (i < n) { ...; i++; }?

    1. for (int i = 1; i <= n + 1; i++) { ... }
    2. for (int i = 0; i <= n; i++) { ... }
    3. for (int i = n; i > 0; i++) { ... }
    4. for (int i = 0; i < n; i++) { ... }

    Answer: D. for (int i = 0; i < n; i++) { ... } A for loop combines initialization, condition and update.

Practice all 282 questions

More AP Computer Science A

Studying for a test in your own class? Make a practice test from your class notes.