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

    0

    10

    14

    17

    Show answer

    Answer: (B)

    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.

  4. Question 4

    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?

    10

    20

    30

    40

    Show answer

    Answer: (B)

    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.

  5. Question 5

    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?

    14

    24

    27

    60

    Show answer

    Answer: (B)

    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.

  6. Question 6

    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.

  7. Question 7

    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.

  8. Question 8

    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.

  9. Question 9

    Consider the following code segment.

    </>Java
    int x = /* missing initialization */;
    int y = /* missing initialization */;
    boolean isMatch = (x >= 10) != (y <= 5);

    Which of the following initializations will cause isMatch to evaluate to true?

    int x = 10; int y = 5;

    int x = 10; int y = 6;

    int x = -1; int y = 6;

    int x = 11; int y = 4;

    Show answer

    Answer: (B)

    Tracing the relational operators identifies the condition that satisfies the expression. Since 10 >= 10 is true and 6 <= 5 is false, the expression true != false correctly evaluates to true.

  10. Question 10

    Consider the following code segment.

    </>Java
    String s1 = /* missing initialization */;
    String s2 = /* missing initialization */;
    boolean result = (s1 == s2) != s1.equals(s2);

    Which of the following initial conditions will cause result to evaluate to true?

    s1 and s2 reference the exact same object containing identical text.

    s1 and s2 reference two distinct objects containing identical text.

    s1 and s2 reference two distinct objects containing different text.

    s1 and s2 reference two distinct objects with different text lengths.

    Show answer

    Answer: (B)

    Recognizing that == compares object references while .equals() compares string contents reveals the correct state. When variables point to distinct objects with identical text, s1 == s2 is false but s1.equals(s2) is true, making false != true evaluate to true.

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 1128 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