AP Computer Science A Unit 2 Exam (2025) - Selection & Iteration

AP Computer Science A Unit 2 Exam (2025) - Selection & Iteration

Prerequisite: Before attempting this exam, make sure you have mastered AP Computer Science A Unit 1 - Objects, Methods & Expressions , as Unit 2 builds directly on those concepts.

Looking for written practice? SKIP TO: AP CSA Unit 2 Free-Response Practice (FRQs) to strengthen your loop and conditional reasoning.

This AP Computer Science A Unit 2 exam is designed for the new 2025 AP CSA 4-Unit curriculum and focuses on decision-making and repetition, two of the most heavily tested skills on the AP exam.

Unit 2 builds on foundational Java syntax by teaching programs how to make decisions using boolean expressions and repeat actions using loops. These skills appear extensively in both multiple-choice questions and free-response questions.

This assessment includes high-difficulty multiple-choice questions modeled after:

  • ✔ The official AP Computer Science A Course and Exam Description (CED)
  • ✔ Barron's AP CSA loop-tracing and logic challenges
  • ✔ CodeHS and AP Classroom progress-check style problems
  • ✔ Common AP CSA mistakes involving conditionals, short-circuit logic, and loop errors

The questions emphasize precise tracing, boolean reasoning, and the ability to recognize loop patterns and pitfalls under exam pressure.

Unit 2 Topics Covered:
  • Boolean expressions and logical operators (&&, ||, !)
  • Relational operators and compound conditions
  • if, else-if, else, and nested conditionals
  • Short-circuit evaluation
  • while loops and for loops
  • Common loop patterns (counting, summing, searching)
  • Off-by-one errors and infinite loops
  • Tracing and debugging selection + iteration logic

At the bottom of this exam, you will complete two concise Free-Response Questions (FRQs) written in the style of AP Classroom Progress Checks, where you will write and test code using a Java sandbox.

Exam Tip: Many Unit 2 errors on the AP exam come from mis-grouped boolean expressions, incorrect loop boundaries, and misunderstanding short-circuit evaluation. Trace carefully.
Time Left: 45:00

AP Computer Science A - Unit 2 Exam

Selection & Iteration (High Difficulty)

Section I: Multiple Choice (30 Questions)

Question 1

What is printed?

int x = 0;
if (x != 0 && 10 / x > 1)
    System.out.println("Yes");
else
    System.out.println("No");
Answer: B
Short-circuit evaluation prevents division by zero.

Question 2

How many times does the loop execute?

for (int i = 2; i <= 20; i += 3)
    System.out.print(i + " ");
Answer: B
2,5,8,11,14,17 → 6 iterations.

Question 3

Which condition checks if x is outside 1-10 inclusive?

Answer: A
Outside a range requires OR.

Question 4

What is printed?

int n = 5;
while (n > 0) {
    n -= 2;
    System.out.print(n + " ");
}
Answer: A
Loop stops when n becomes negative.

Question 5

What is printed?

String s = "java";
for (int i = 0; i < s.length(); i++)
    if (s.charAt(i) == 'a')
        System.out.print(i + " ");
Answer: B
'a' appears at indices 1 and 3.

Question 6

Which statement about loops is TRUE?

Answer: C
All correct loops must terminate.

Question 7

What is printed?

int x = 10;
if (x > 5)
    if (x < 8)
        System.out.println("A");
    else
        System.out.println("B");
Answer: B
Else pairs with the nearest if.

Question 8

What is printed?

int sum = 0;
for (int i = 1; i <= 4; i++)
    sum += i;
System.out.println(sum);
Answer: B
1+2+3+4 = 10.

Question 9

Which expression generates a random integer from 1-6 inclusive?

Answer: B
Correct inclusive random pattern.

Question 10

What is printed?

String s = "hello";
s.toUpperCase();
System.out.println(s);
Answer: A
Strings are immutable.

Question 11

What is printed?

int x = 1;
while (x < 10) {
    x *= 2;
}
System.out.println(x);
Answer: C
1 → 2 → 4 → 8 → 16, then condition fails.

Question 12

Which expression is equivalent to !(a >= 5 && a <= 10)?

Answer: A
De Morgan's Law: NOT (AND) → OR of negations.

Question 13

What is printed?

int count = 0;
for (int i = 1; i <= 10; i++) {
    if (i % 4 == 0)
        count++;
}
System.out.println(count);
Answer: A
Only 4 and 8 are divisible by 4.

Question 14

What is printed?

int x = 3;
for (int i = 0; i < x; i++) {
    x--;
    System.out.print(i + " ");
}
Answer: A
x decreases, causing early termination.

Question 15

Which statement about while loops is TRUE?

Answer: C
If the condition starts false, the loop never runs.

Question 16

What is printed?

String s = "APCSA";
for (int i = 1; i < s.length(); i += 2)
    System.out.print(s.charAt(i));
Answer: A
Indices 1,3,5 → P, S, A.

Question 17

What causes an infinite loop?

Answer: C
If nothing changes, the condition never becomes false.

Question 18

What is printed?

int x = 10;
if (x < 5 || x / 2 > 3)
    System.out.println("A");
else
    System.out.println("B");
Answer: A
10 / 2 = 5, which is greater than 3.

Question 19

What is printed?

int n = 7;
int count = 0;
while (n > 0) {
    n -= 3;
    count++;
}
System.out.println(count);
Answer: B
7 → 4 → 1 → -2 → 3 iterations.

Question 20

Which condition checks if x equals 2, 4, or 6?

Answer: C
Each equality must be explicitly stated.

Question 21

What is printed?

for (int i = 5; i > 0; i--) {
    if (i == 3)
        break;
    System.out.print(i + " ");
}
Answer: B
Loop stops when i equals 3.

Question 22

Which best describes short-circuit evaluation?

Answer: B
Evaluation stops once result is known.

Question 23

What is printed?

int sum = 0;
for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 2; j++)
        sum++;
System.out.println(sum);
Answer: B
3 x 2 = 6 total iterations.

Question 24

What is printed?

int x = 1;
while (x <= 5) {
    System.out.print(x + " ");
    x += 2;
}
Answer: A
x increases by 2 each time.

Question 25

Which loop counts from 10 down to 1?

Answer: B
Starts at 10 and decrements to 1.

Question 26

What is printed?

int x = 4;
int y = 0;
while (x > 0) {
    y++;
    x /= 2;
}
System.out.println(y);
Answer: B
4 → 2 → 1 → 0 → 3 iterations.

Question 27

Which statement about break is TRUE?

Answer: B
break exits the innermost loop immediately.

Question 28

What is printed?

int count = 0;
for (int i = 1; i <= 20; i++) {
    if (i % 2 == 0 && i % 5 == 0)
        count++;
}
System.out.println(count);
Answer: B
10 and 20 satisfy both conditions.

Question 29

What is printed?

int i = 0;
while (i < 5)
    System.out.print(i++);
Answer: A
Post-increment prints before incrementing.

Question 30

Which scenario BEST uses a sentinel-controlled loop?

Answer: B
Sentinel loops terminate on a specific value.

Section II: Free Response (Progress Check Style)

FRQ 1 - Loop Logic

Write a method countMultiples that returns how many integers between start and end inclusive are divisible by k.

public static int countMultiples(int start, int end, int k)

FRQ 2 - String Traversal

Write a method hasAlternatingCase that returns true if a string alternates uppercase and lowercase letters starting at index 0.

public static boolean hasAlternatingCase(String s)

Next Step: Unit 3

Once you are comfortable with selection and iteration, you are ready to move on to

AP Computer Science A Unit 3 - Class Creation

, where you will design classes, write constructors, and define methods.

Want to test everything together? Try our >

Full AP CSA Practice Exam

for a timed, exam-length experience.

Stuck on loops or conditionals? Explore

AP CSA tutoring for loops and conditionals

with step-by-step guidance.

Contact form