---
title: "AP Computer Science A Unit 2 Practice Test"
description: "A timed AP Computer Science A Unit 2 (Selection and Iteration) practice test in the AP format, drawn from 1,128 multiple-choice questions and 34 FRQs written for the unit. The standard version takes about 66 minutes."
canonical: "https://fiveable.me/ap-comp-sci-a/unit-test/2"
type: "unit-test"
subject: "AP Computer Science A"
unit: "Unit 2"
---

# AP Computer Science A Unit 2 Practice Test

## Overview

A timed AP Computer Science A Unit 2 (Selection and Iteration) practice test in the AP format, drawn from 1,128 multiple-choice questions and 34 FRQs written for the unit. The standard version takes about 66 minutes.

## Test Format

- 20 Multiple Choice Questions (43 minutes)
- 1 FRQ – FRQ 1 – Methods and Control Structures (23 minutes)

## Unit FRQs by Type

- [FRQ 1 – Methods and Control Structures](/ap-comp-sci-a/frqs/methods-and-control-structures?unit=2): 34 questions

## Practice Questions

### Question 1

**Countdown Accumulator with while Loop**

> Consider the following Java code.
>
> ```java
> public class CountDown {
>     public static int compute(int n) {
>         int total = 0;
>         int k = n;
>         while (k > 0) {
>             total += k;
>             k -= 2;
>         }
>         return total;
>     }
>
>     public static void main(String[] args) {
>         int result = compute(7);
>         System.out.println(result);
>
>         int x = 0;
>         int limit = 4;
>         while (x < limit) {
>             System.out.print(x + " ");
>             x++;
>         }
>     }
> }
> ```

Source: Fiveable fictional Java practice code aligned to the AP Computer Science A Fall 2025 CED

What is printed to the console as a result of executing the main method in the code shown?

- (A) 16
0 1 2 3
- (B) 16
0 1 2 3 4
- (C) 12
0 1 2 3
- (D) 16
1 2 3 4

**Answer: (A)** Tracing compute(7): iteration 1 k=7, total=7, k becomes 5; iteration 2 k=5, total=12, k becomes 3; iteration 3 k=3, total=15, k becomes 1; iteration 4 k=1, total=16, k becomes -1; condition -1>0 is false, loop exits, returns 16. println(16) prints '16' then newline. Second loop: x starts at 0, limit=4. Prints '0 ', '1 ', '2 ', '3 ', then x=4 fails x<4, loop ends. Full output is '16' on line 1 and '0 1 2 3 ' on line 2.

### Question 2

Consider the following code segment.

```java
int count = 0;
for (int i = 0; i < 4; i++) {
    for (int j = i; j < 4; j++) {
        if ((i + j) % 2 == 0) {
            count++;
        }
    }
}
System.out.println(count);
```

What is printed as a result of executing this code segment?

- (A) `6`
- (B) `8`
- (C) `10`
- (D) `16`

**Answer: (A)** Tracing the nested loops shows the inner loop's starting value depends on the outer loop variable `i`. The condition `(i + j) % 2 == 0` is true when `i` and `j` are both even or both odd. The matching `(i, j)` pairs are (0,0), (0,2), (1,1), (1,3), (2,2), and (3,3), which increments `count` a total of 6 times.

### Question 3

Consider the following code segment.

```java
String result = "";
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= i; j++) {
        result += (i * j) + " ";
    }
}
System.out.println(result);
```

What is printed as a result of executing this code segment?

- (A) `"1 2 4 3 6 9 "`
- (B) `"1 2 2 4 3 6 "`
- (C) `"1 2 2 3 3 3 "`
- (D) `"1 1 2 1 2 3 "`

**Answer: (A)** The outer loop runs from 1 to 3, and the inner loop runs from 1 to the current value of `i`. The code concatenates the product of `i` and `j`. For `i=1`, it appends 1*1. For `i=2`, it appends 2*1 and 2*2. For `i=3`, it appends 3*1, 3*2, and 3*3.

### Question 4

Consider the following code segment.

```java
int val = 20;
for (int i = 1; i < 4; i++) {
    int j = i;
    while (j > 0) {
        val -= j;
        j--;
    }
}
System.out.println(val);
```

What is printed as a result of executing this code segment?

- (A) `10`
- (B) `14`
- (C) `0`
- (D) `17`

**Answer: (A)** The outer `for` loop runs for `i` values 1, 2, and 3. The inner `while` loop subtracts all integers from `i` down to 1 from `val`. It subtracts 1 (when i=1), then 2+1=3 (when i=2), then 3+2+1=6 (when i=3). The total subtracted is 10, leaving `val` as 10.

### Question 5

Consider the following code segment.

```java
int total = 0;
for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= 4; j++) {
        if (j > i) {
            break;
        }
        total += j;
    }
}
System.out.println(total);
```

What is printed as a result of executing this code segment?

- (A) `20`
- (B) `10`
- (C) `30`
- (D) `40`

**Answer: (A)** The `break` statement causes the inner loop to terminate early when `j > i`. This means `j` only takes on values from 1 up to `i`. The code sums these values: 1 (for i=1), 1+2 (for i=2), 1+2+3 (for i=3), and 1+2+3+4 (for i=4), resulting in 1 + 3 + 6 + 10 = 20.

### Question 6

Consider the following code segment.

```java
double val = 0;
int divisor = 2;
for (int i = 0; i < 2; i++) {
    val += 11 / divisor;
    divisor += 3;
}
System.out.println(val);
```

What is printed as a result of executing this code segment?

- (A) 7.0
- (B) 7.7
- (C) 8.0
- (D) 5.0

**Answer: (A)** Tracing a loop containing integer division and automatic widening. In the first iteration, `11 / 2` evaluates to `5` (integer division), which widens to `5.0`. In the second iteration, `11 / 5` evaluates to `2`, widening to `2.0`. The sum is `7.0`.

### Question 7

Consider the following code segment.

```java
int total = 0;
int count = 5;
while (count > 2) {
    total += count;
    count--;
}
total *= count;
System.out.println(total);
```

What is printed as a result of executing this code segment?

- (A) 24
- (B) 14
- (C) 27
- (D) 60

**Answer: (A)** Analyzing loop execution with decrement operators requires tracking the loop control variable and the accumulator. The loop runs for `count` values of 5, 4, and 3, adding them to `total` to reach 12. The loop terminates when `count` becomes 2, and the final statement multiplies `total` (12) by the current `count` (2) to yield 24.

### Question 8

The following method is intended to return a new string containing the characters of `word` in reverse order.

```java
public static String reverse(String word) {
    String result = "";
    /* missing code */
    return result;
}
```

Which of the following can replace `/* missing code */` to correctly reverse the string?

- (A) `for (int i = 0; i < word.length(); i++) { result = word.charAt(i) + result; }`
- (B) `for (int i = 0; i < word.length(); i++) { result = result + word.charAt(i); }`
- (C) `for (int i = 1; i < word.length(); i++) { result = word.charAt(i) + result; }`
- (D) `for (int i = 1; i <= word.length(); i++) { result = result + word.charAt(i); }`

**Answer: (A)** Evaluating string concatenation order within a forward-iterating loop. By iterating from the beginning of the string and prepending each character (`word.charAt(i) + result`), the characters are accumulated in reverse order.

### Question 9

The following method is intended to return a new string with all lowercase vowels (`"a"`, `"e"`, `"i"`, `"o"`, `"u"`) removed from the parameter `word`.

```java
public static String removeVowels(String word) {
    String result = "";
    for (int i = 0; i < word.length(); i++) {
        String letter = word.substring(i, i + 1);
        if (/* missing code */) {
            result += letter;
        }
    }
    return result;
}
```

Which of the following can replace `/* missing code */` so the method works as intended?

- (A) `"aeiou".indexOf(letter) == -1`
- (B) `"aeiou".indexOf(letter) >=  0`
- (C) `"aeiou".indexOf(letter) != -1`
- (D) `"aeiou".indexOf(letter) ==  1`

**Answer: (A)** Utilizing the `indexOf` method to filter characters. The `indexOf` method returns `-1` when a substring is not found within the target string. Checking for `-1` ensures the current letter is not a vowel before appending it to the result.

### Question 10

A meteorologist has two datasets: Dataset 1 contains `cityID` and `averageTemp` (a `double`). Dataset 2 contains `cityID` and `currentTemp` (a `double`). The meteorologist evaluates the Boolean expression `currentTemp <= averageTemp - 10.0` for a specific city. What knowledge is extracted when this expression evaluates to `true`?

- (A) The city's current temperature is at least ten degrees below its historical average.
- (B) The city's current temperature is exactly ten degrees below its historical average.
- (C) The city's current temperature is at most ten degrees above its historical average.
- (D) The city's current temperature is more than ten degrees above its historical average.

**Answer: (A)** Evaluating the `<=` operator requires recognizing that it checks for values less than or equal to a threshold. Since the threshold is the average temperature minus 10.0, a true result indicates the current temperature is at least ten degrees colder than the historical average.

## Review the Unit

- [2.1 Algorithms with Selection and Repetition](/ap-comp-sci-a/unit-2/algorithms-with-selection-and-repetition/study-guide/42crNSZyW8IRsntk9IHe)
- [2.2 Boolean Expressions](/ap-comp-sci-a/unit-2/boolean-expressions/study-guide/s6j4i9ram3AlCg3uYjwd)
- [2.3 If Statements and Control Flow](/ap-comp-sci-a/unit-2/if-statements-and-control-flow/study-guide/IbxsFeJQ5T1FNNBEWErO)
- [2.4 If-Else Statements](/ap-comp-sci-a/unit-2/if-else-statements/study-guide/t6FfmE9rwA4mdWqv2ogR)
- [2.5 Compound Boolean Expressions](/ap-comp-sci-a/unit-2/compound-boolean-expressions/study-guide/WVkDsh43kiP30BIqg3Sh)
- [2.6 Equivalent Boolean Expressions](/ap-comp-sci-a/unit-2/equivalent-boolean-expressions/study-guide/aMDnyFuOcAXnZigLW1vL)
- [2.7 While Loops](/ap-comp-sci-a/unit-2/while-loops/study-guide/7qGsGOh1UKALAWpJhZOi)
- [2.8 For Loops](/ap-comp-sci-a/unit-2/for-loops/study-guide/DJuLxKz6SiSAX2cEVmCt)
- [2.9 Implementing Selection and Iteration Algorithms](/ap-comp-sci-a/unit-2/implementing-selection-and-iteration-algorithms/study-guide/ulqF0nPukr6rbwgDTCuU)
- [2.10 Developing Algorithms Using Strings](/ap-comp-sci-a/unit-2/developing-algorithms-using-strings/study-guide/hDOL1VhnMQFPkBf6xMMW)
- [2.11 Nested Iteration](/ap-comp-sci-a/unit-2/nested-iteration/study-guide/Buapg1KURHNbw6yRY8EZ)
- [2.12 Informal Code Analysis](/ap-comp-sci-a/unit-2/informal-code-analysis/study-guide/CR84MbOE4FDDoSVokDVZ)

## Other Unit Tests

- [Unit 1: Using Objects and Methods](/ap-comp-sci-a/unit-test/1)
- [Unit 3: Class Creation](/ap-comp-sci-a/unit-test/3)
- [Unit 4: Data Collections](/ap-comp-sci-a/unit-test/4)

## About This Document

Canonical Fiveable pages are available as Markdown at the same path plus `.md`.

- [llms.txt](https://fiveable.me/llms.txt): index of Fiveable's sections and URL patterns
- [llms-full.txt](https://fiveable.me/llms-full.txt): complete subject and unit listing
- [MCP server](https://fiveable.me/mcp): call Fiveable as tools instead of fetching pages (`https://fiveable.me/api/mcp`)
- [MCP server for AP teachers](https://fiveable.me/mcp/teachers): a teacher's classes, assignments and AP-rubric grading (`https://fiveable.me/api/mcp/teacher`)
