AtlasEntrar
Black FridayHalf off Premium, through Sunday, Nov 29.See plans
AP Computer Science A · Unit test

Selection and iteration practice test

Up to 15 AP Computer Science A questions on selection and iteration, timed at the exam's pace and scored, new every time. Try 3 below first.

Take the selection and iteration unit test →Try 3 questions

Selection and iteration is 25%–35% of the AP Computer Science A exam's multiple choice. Learn it first in the selection and iteration chapter.

3 free selection and iteration practice questions

Real questions from the Atlas AP Computer Science A exam pool, with the answer and the working.

  1. Consider the following code segment.

    int total = 0;
    int i;
    for (i = 1; i < 10; i += 3)
    {
      total += i;
    }
    System.out.println(total + " " + i);

    What is printed as a result of executing the code segment?

    1. A12 7
    2. B12 10
    3. C22 13
    4. D11 10
    Show the answer

    B.

    The body runs for i = 1, 4, 7, so total = 12. After the last pass i becomes 10, the test 10 < 10 fails, and i keeps that value after the loop.

    12 7: prints the last value used in the body: i is increased once more, to 10, and then the test fails. 22 13: runs the body once more: 10 < 10 is false. 11 10: skips the first value, 1: the update runs after the body.

  2. Consider the following code segment.

    int count = 0;
    for (int i = 1; i <= 5; i++)
    {
      for (int j = i; j <= 5; j++)
      {
        if ((i + j) % 3 == 0)
        {
          count++;
        }
      }
    }
    System.out.println(count);

    What is printed as a result of executing the code segment?

    1. A3
    2. B4
    3. C5
    4. D9
    Show the answer

    C.

    The loops visit each pair with 1 ≤ i ≤ j ≤ 5 once. Of those, 5 have a sum divisible by 3.

    4: starts j at i + 1: it starts at i, so pairs like (i, i) are counted too. 3: stops both loops before 5: the tests use <=. 9: starts j at 1, counting both (i, j) and (j, i).

  3. Assume x and y are properly declared and initialized int variables. Which of the following expressions is equivalent to the expression below for all values of x and y?

    !(x > 5 || y <= 3)
    1. Ax <= 5 || y > 3
    2. Bx < 5 && y > 3
    3. Cx <= 5 && y > 3
    4. Dx > 5 && y <= 3
    Show the answer

    C.

    By De Morgan's law, !(A || B) is !A && !B. The negation of x > 5 is x <= 5, and the negation of y <= 3 is y > 3.

    x <= 5 || y > 3 negates each part but keeps ||. x < 5 && y > 3 gets the boundary wrong: it is false when x is 5. x > 5 && y <= 3 changes the operator without negating the parts.

Take the full unit test →AP Computer Science A practice exam

Questions people ask

What is in the AP Computer Science A Selection and iteration practice test?
Up to 15 multiple-choice questions from this unit, plus a free-response question when the unit has one, timed at the real exam's pace and scored as a percent. The questions are new every time, drawn from a pool of checked questions.
How much of the AP Computer Science A exam is Selection and iteration?
25% to 35% of the multiple-choice questions, by the College Board’s course and exam description.
Is it free?
The 3 questions on this page need no account. With a free Atlas account your first unit test is free; Atlas Premium has no limit.

Other AP Computer Science A units