AtlasĐăng nhập
Black FridayHalf off Premium, through Sunday, Nov 29.See plans
AP Computer Science Principles · Unit test

Algorithms and programming practice test

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

Take the algorithms and programming unit test →Try 3 questions

Algorithms and programming is 30%–35% of the AP Computer Science Principles exam's multiple choice. Learn it first in the algorithms and programming chapter.

3 free algorithms and programming practice questions

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

  1. Consider the following code segment.

    vals ← [4, 9, 2, 7, 5, 3, 6]
    sum ← 0
    i ← 1
    REPEAT UNTIL(i > LENGTH(vals))
    {
      sum ← sum + vals[i]
      i ← i + 2
    }
    DISPLAY(sum)

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

    1. A11
    2. B16
    3. C17
    4. D19
    Show the answer

    C.

    Lists start at index 1. i takes the values 1, 3, 5, 7 (it stops when it passes 7), so the elements added are 4 + 2 + 5 + 6 = 17.

    11 stops before i reaches LENGTH(vals), leaving out the last element when i lands on it; 16 adds the indices i instead of the elements vals[i]; 19 treats index 1 as the second element, as if the list started at index 0.

  2. Consider the following code segment.

    nums ← [3, 4, 6, 7, 8, 9]
    i ← 1
    REPEAT UNTIL(i > LENGTH(nums))
    {
      IF(nums[i] MOD 2 = 0)
      {
        REMOVE(nums, i)
      }
      i ← i + 1
    }
    DISPLAY(nums)

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

    1. A[3, 7, 9]
    2. B[3, 4, 7, 8]
    3. C[4, 6, 8]
    4. D[3, 6, 7, 9]
    Show the answer

    D.

    After REMOVE(nums, i), the next element shifts into position i, but i still increases, so that element is never checked. Neighboring even values therefore survive: the result is [3, 6, 7, 9].

    [3, 7, 9] is what the programmer probably intended (every even value removed), but the loop skips the element that moves into position i after a removal; [3, 4, 7, 8] treats REMOVE(nums, i) as removing the element after position i, as if indices started at 0; [4, 6, 8] keeps the even values instead of removing them.

  3. Consider the following code segment.

    PROCEDURE mystery(n)
    {
      result ← 0
      REPEAT UNTIL(n = 0)
      {
        result ← result * 10 + n MOD 10
        n ← (n - n MOD 10) / 10
      }
      RETURN(result)
    }
    DISPLAY(mystery(472))

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

    1. A13
    2. B274
    3. C472
    4. D2740
    Show the answer

    B.

    Each pass takes the last digit (n MOD 10), appends it to result (result × 10 + digit), and removes it from n. The digits of 472 come off in the order 2, 7, 4, so the procedure returns 274: the digits reversed.

    13 adds the digits, missing the multiplication by 10; 472 builds the digits in their original order; 2740 makes one extra pass, which multiplies the result by 10.

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

Questions people ask

What is in the AP Computer Science Principles Algorithms and programming 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 Principles exam is Algorithms and programming?
30% 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 Principles units