Unit 2 — Selection and Iteration

Unit 2 is about controlling program flow: making decisions (booleans, if) and repeating steps (loops). Study the topics first, then take the Unit 2 quiz: 15 MCQ + 1 FRQ.

How to use the quiz

  • Answer MCQs first. Don’t check as you go.
  • Click Check Answers to see score + explanations.
  • Try the FRQ, then reveal the sample solution.

Unit 2 Quiz — Multiple Choice (15)

Choose the best answer. (One correct option each.)

1) Which statement best describes a loop?

Topic 2.1

2) Which expression evaluates to true when x is 7?

Topic 2.2
int x = 7;

3) What is the value of y after the code executes?

Topic 2.3
int x = 5;
int y = 0;
if (x > 3) {
  y = 10;
}

4) Consider the code segment. What is printed?

Topic 2.4
int a = 4;
if (a > 2) {
  if (a < 4) {
    System.out.print("X");
  } else {
    System.out.print("Y");
  }
} else {
  System.out.print("Z");
}

5) Which expression is equivalent to “x is between 1 and 10 inclusive”?

Topic 2.5

6) Assume p and q are boolean expressions. Which is always equivalent to !(p && q)?

Topic 2.6

7) How many times does the loop body execute?

Topic 2.7
int i = 0;
while (i < 3) {
  i++;
}

8) What is the final value of sum?

Topic 2.8
int sum = 0;
for (int k = 1; k <= 4; k++) {
  sum += k;
}

9) Which is a correct reason to use a while loop instead of a for loop?

Topic 2.7–2.8

10) What is printed?

Topic 2.9
int n = 5;
if (n % 2 == 0) {
  System.out.print("E");
} else {
  System.out.print("O");
}

11) Consider the code segment. What is the value of count after it executes?

Topic 2.9
int count = 0;
for (int i = 0; i < 5; i++) {
  if (i % 2 == 0) {
    count++;
  }
}

12) Which code segment correctly counts how many times the letter 'a' appears in s?

Topic 2.10
String s = "banana";

13) What is the output of the code segment?

Topic 2.11
int total = 0;
for (int r = 0; r < 2; r++) {
  for (int c = 0; c < 3; c++) {
    total++;
  }
}
System.out.print(total);

14) Which describes informal run-time analysis in AP CSA terms?

Topic 2.12

15) What is the value of x after the code executes?

Topic 2.7
int x = 10;
while (x > 0) {
  x -= 4;
}

Unit 2 Quiz — FRQ (1)

This FRQ focuses on selection + iteration (Unit 2 skills).

FRQ 1) countMultiples

Write a static method countMultiples that counts how many integers in the range 1 through limit (inclusive) are multiples of k.

  • Method header: public static int countMultiples(int limit, int k)
  • Return the count of integers n such that 1 <= n <= limit and n % k == 0.
  • Assume limit >= 1 and k >= 1.
  • Example: countMultiples(10, 3) returns 3 (3, 6, 9).

Write your solution here: