Skip to main content

AP Computer Science A Unit 2 practice questions

Try 10 multiple-choice questions from Unit 2 here. Pick an answer to see why it's right or wrong, then take the timed test above for a full AP-style set.

  1. Question 1

    Source:

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

    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++;
            }
        }
    }

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

    16 0 1 2 3

    16 0 1 2 3 4

    12 0 1 2 3

    16 1 2 3 4

    Show answer

    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.

  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?

    6

    8

    10

    16

    Show answer

    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.

  3. 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?

    "1 2 4 3 6 9 "

    "1 2 2 4 3 6 "

    "1 2 2 3 3 3 "

    "1 1 2 1 2 3 "

    Show answer

    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 11. For i=2, it appends 21 and 22. For i=3, it appends 31, 32, and 33.

  4. 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?

    10

    14

    0

    17

    Show answer

    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.

  5. 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?

    20

    10

    30

    40

    Show answer

    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.

  6. 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?

    7.0

    7.7

    8.0

    5.0

    Show answer

    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.

  7. 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?

    24

    14

    27

    60

    Show answer

    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.

  8. 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?

    for (int i = 0; i < word.length(); i++) { result = word.charAt(i) + result; }

    for (int i = 0; i < word.length(); i++) { result = result + word.charAt(i); }

    for (int i = 1; i < word.length(); i++) { result = word.charAt(i) + result; }

    for (int i = 1; i <= word.length(); i++) { result = result + word.charAt(i); }

    Show answer

    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.

  9. 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?

    "aeiou".indexOf(letter) == -1

    "aeiou".indexOf(letter) >= 0

    "aeiou".indexOf(letter) != -1

    "aeiou".indexOf(letter) == 1

    Show answer

    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.

  10. 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?

    The city's current temperature is at least ten degrees below its historical average.

    The city's current temperature is exactly ten degrees below its historical average.

    The city's current temperature is at most ten degrees above its historical average.

    The city's current temperature is more than ten degrees above its historical average.

    Show answer

    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.

What's on the AP Computer Science A Unit 2 practice test

The standard version takes about 66 minutes and follows the timing and directions of the AP exam, sized to one unit:

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

Questions are drawn from 987 multiple-choice questions and 34 FRQs written for Unit 2, Selection and Iteration.

Practice AP Computer Science A Unit 2 FRQs

Answer the unit's free-response questions one at a time and get a score on every part.

Review Unit 2 before you start

Other AP Computer Science A unit tests