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.
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.
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?
- A12 7
- B12 10
- C22 13
- 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.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?
- A3
- B4
- C5
- 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).Assume
xandyare properly declared and initializedintvariables. Which of the following expressions is equivalent to the expression below for all values ofxandy?!(x > 5 || y <= 3)- A
x <= 5 || y > 3 - B
x < 5 && y > 3 - C
x <= 5 && y > 3 - D
x > 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 > 3negates each part but keeps ||.x < 5 && y > 3gets the boundary wrong: it is false when x is 5.x > 5 && y <= 3changes the operator without negating the parts.- A
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.