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.
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.
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?
- A11
- B16
- C17
- 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.
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?
- A[3, 7, 9]
- B[3, 4, 7, 8]
- C[4, 6, 8]
- 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.
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?
- A13
- B274
- C472
- 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.
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.