Skip to main content

AP Computer Science A Unit 2: Selection and Iteration

Ready When You Are

Select a course and unit, then start practicing.

Selection and Iteration connects to later units in AP Computer Science A. Solid practice now reduces rework when those ideas reappear in combined scenarios.

Control flow: conditionals and loops for algorithm implementation and problem solving. On the AP Computer Science A exam, items from Selection and Iteration 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 if, if-else, nested conditionals; for, while, do-while loops and loop invariants; Combining selection and iteration to implement algorithms; and Debugging and tracing control flow.

On the AP Computer Science A exam, Selection and Iteration is weighted at roughly 25%–35% 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.

Students sometimes overfit Selection and Iteration to one classroom example. AP writers deliberately vary context; practice explaining the underlying rule so it travels to new settings.

When you miss a question, tag it by skill (vocabulary, calculation, inference). Patterns in your misses show where to reread your notes.

Keep a running list of miss types for Selection and Iteration—vocabulary, setup, or inference. Three sessions with the same miss pattern mean reread that thread in your notes before practicing more questions.

Click Practice above for fresh MCQs tied to Selection and Iteration. 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 code segment.

    int battery = 40;
    int recharge = 5;
    
    for (int trip = 1; trip <= 3; trip++) {
        if (battery < 50) {
            battery += recharge;
            recharge += 3;
        } else {
            battery -= 8;
        }
    }
    
    System.out.print(battery + " " + recharge);

    What is printed when the code segment executes?

    • A

      45 8

    • B

      53 8

    • C

      45 11

      Correct answer
    • D

      53 11

    Explanation

    The loop executes 3 times, and the condition is checked using the current value of battery each time.

    First iteration: battery = 40, so the if branch runs. battery becomes 45 and recharge becomes 8. Second iteration: battery = 45, so the if branch runs again. battery becomes 53 and recharge becomes 11. Third iteration: battery = 53, so the else branch runs. battery becomes 45, and recharge stays 11.

    Therefore, the program prints 45 11.

    C is correct. D is incorrect because it ignores the subtraction in the third iteration. A is incorrect because recharge is updated during the first two iterations. B is incorrect because it combines the final battery after subtraction with an outdated recharge value.

  2. Question 2

    A packing program uses large crates that hold 88 items and small crates that hold 33 items. It repeatedly packs a large crate when possible; otherwise, it packs a small crate. Any items left when fewer than 33 remain are unpacked. Consider the following code:

    int remaining = 53;
    int large = 0;
    int small = 0;
    
    while (remaining >= 3) {
        if (remaining >= 8) {
            remaining -= 8;
            large++;
        } else {
            remaining -= 3;
            small++;
        }
    }

    Which statement correctly gives a loop invariant at the start of every loop iteration and the values of the variables when the loop terminates?

    • A

      At the start of every iteration, 53=8(large)+3(small)+remaining53 = 8(large) + 3(small) + remaining and remaining≥3remaining \geq 3. At termination, large=6large=6, small=1small=1, and remaining=2remaining=2.

      Correct answer
    • B

      At the start of every iteration, 53=8(large)+3(small)+remaining53 = 8(large) + 3(small) + remaining and remaining≥0remaining \geq 0. At termination, large=6large=6, small=1small=1, and remaining=2remaining=2.

    • C

      At the start of every iteration, 53=8(large)+3(small)+remaining53 = 8(large) + 3(small) + remaining and remaining≥3remaining \geq 3. At termination, large=5large=5, small=1small=1, and remaining=10remaining=10.

    • D

      At the start of every iteration, 53=8(large)+3(small)53 = 8(large) + 3(small) and remaining≥3remaining \geq 3. At termination, large=6large=6, small=1small=1, and remaining=2remaining=2.

    Explanation

    Each crate reduces remaining by exactly the number of items it packs and increases the corresponding crate count by one. Therefore, the packed items plus the items still unpacked always total 5353. At the start of each iteration, the loop condition also guarantees remaining≥3remaining \geq 3. The program packs six large crates, reducing 5353 to 55, then one small crate, leaving 22; the loop ends.

    A. Correct. It states both the conservation relationship and the loop condition that holds at the start of each iteration, and gives the correct final values.

    D. Incorrect. It omits remaining from the conservation relationship, so the stated equality fails whenever unpacked items remain.

    C. Incorrect. After six large crates and one small crate, remaining is 22, not 1010; the loop condition is then false.

    B. Incorrect. The conservation relationship and final values are right, but remaining≥0remaining \geq 0 alone is not the stated condition at the start of every iteration: the loop body runs only when remaining≥3remaining \geq 3.

  3. Question 3

    What is printed when the following code segment is executed?

    int a = 12;
    int b = 3;
    
    do {
        if (a % b == 0) {
            a = a / b;
            if (a % 2 == 0) {
                b++;
            } else {
                b += 2;
            }
        } else {
            a--;
        }
    } while (a > 2);
    
    System.out.print(a + "," + b);
    • A

      2,6

    • B

      1,6

      Correct answer
    • C

      1,4

    • D

      4,6

    Explanation

    The correct answer is B.

    The loop is a do-while loop, so the body executes at least once.

    • First iteration: a = 12 and b = 3, so 12 % 3 == 0. Set a = 12 / 3 = 4. Then 4 % 2 == 0, so b++ makes b = 4.
    • Since a = 4, the condition a > 2 is still true, so the loop repeats.
    • Second iteration: 4 % 4 == 0. Set a = 4 / 4 = 1. Then 1 % 2 != 0, so b += 2 makes b = 6.
    • Now a = 1, so a > 2 is false and the loop ends.

    Therefore, the code prints 1,6.

    C is incorrect because it includes the first update to b but misses the second iteration.

    A is incorrect because a does not end at 2; after the second division, a becomes 1.

    D is incorrect because the loop does not stop after the first pass; a is still greater than 2 after the first iteration, so the body runs again.

  4. Question 4

    Consider the following Java code.

    int temp = 8;
    int passes = 0;
    
    do {
        if (temp >= 8) {
            temp -= 3;
        } else {
            temp += 2;
        }
        passes++;
    } while (temp < 8);

    What are the values of temp and passes after the code executes?

    • A

      temp is 9 and passes is 3.

      Correct answer
    • B

      temp is 5 and passes is 1.

    • C

      temp is 8 and passes is 0.

    • D

      temp is 9 and passes is 2.

    Explanation

    A do-while loop executes its body before checking its condition. Starting at temp = 8, the first pass changes temp to 5; the condition is true, so the loop continues. The next two passes change temp from 5 to 7, then from 7 to 9. The condition is then false, and three passes have occurred.

    A. Correct: the final values are temp = 9 and passes = 3. C. Incorrect: this treats the loop like a pretest while loop and assumes the body can be skipped. B. Incorrect: after the first pass, temp < 8 is true, so another pass is required. D. Incorrect: after the second pass, temp is 7, so the loop condition is still true and a third pass is required.

  5. Question 5

    An aquarium monitor stores water temperatures as integers in an ArrayList. A method should return the number of readings from 1515 to 2525 inclusive, but it must exclude readings of 1818 and 2222 because those values were taken during calibration.

    Which code segment correctly completes the method?

    public static int countValidReadings(ArrayList<Integer> readings)
    {
        int count = 0;
        for (int i = 0; i < readings.size(); i++)
        {
            int value = readings.get(i);
            // missing code
        }
        return count;
    }
    • A

      if (value >= 15 && value <= 25 && value != 18 && value != 22) { count++; }

      Correct answer
    • B

      if ((value >= 15 && value <= 25) && (value == 18 || value == 22)) { count++; }

    • C

      if (value > 15 && value < 25 && value != 18 || value != 22) { count++; }

    • D

      if (value >= 15 || value <= 25 || value != 18 || value != 22) { count++; }

    Explanation

    The correct choice is A. The condition uses && to require that a reading satisfy all of the needed rules at the same time: it must be at least 15, at most 25, and it must not be 18 or 22. Only readings that meet every condition are counted.

    D is incorrect because || makes the condition far too broad. Almost any integer will satisfy at least one of those tests, so it would count readings that should be excluded.

    C is incorrect because value != 22 is joined with ||, which makes the whole condition true for almost every value. It also uses > and <, which incorrectly excludes 15 and 25.

    B is incorrect because it counts only the calibration values 18 and 22, which is the opposite of the requirement. The method should count valid readings, not excluded ones.

Frequently asked questions