---
title: "AP Computer Science A Unit 3 Practice Test"
description: "A timed AP Computer Science A Unit 3 (Class Creation) practice test in the AP format, drawn from 681 multiple-choice questions and 31 FRQs written for the unit. The standard version takes about 88 minutes."
canonical: "https://fiveable.me/ap-comp-sci-a/unit-test/3"
type: "unit-test"
subject: "AP Computer Science A"
unit: "Unit 3"
---

# AP Computer Science A Unit 3 Practice Test

## Overview

A timed AP Computer Science A Unit 3 (Class Creation) practice test in the AP format, drawn from 681 multiple-choice questions and 31 FRQs written for the unit. The standard version takes about 88 minutes.

## Test Format

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

## Unit FRQs by Type

- [FRQ 1 – Methods and Control Structures](/ap-comp-sci-a/frqs/methods-and-control-structures?unit=3): 6 questions
- [FRQ 2 – Class Design](/ap-comp-sci-a/frqs/class-design?unit=3): 25 questions

## Practice Questions

### Question 1

Consider the following class definition and `main` method:

```java
public class NumberModifier {
    public static void modify(int a, int b) {
        a = a * 2;
        b = b - 3;
        System.out.print(a + " " + b + " ");
    }
    
    public static void main(String[] args) {
        int a = 4;
        int b = 8;
        modify(a, b);
        System.out.print(a + " " + b);
    }
}
```

What is printed as a result of executing the `main` method?

- (A) The program executes successfully and prints the exact sequence: 8 5 4 8
- (B) The program executes successfully and prints the exact sequence: 8 5 8 5
- (C) The program executes successfully and prints the exact sequence: 4 8 8 5
- (D) The program executes successfully and prints the exact sequence: 4 8 4 8

**Answer: (A)** Tracing the method call requires understanding pass-by-value semantics. When `modify(4, 8)` is called, the local parameters `a` and `b` are initialized to 4 and 8. Inside the method, local `a` becomes 8 and local `b` becomes 5, printing "8 5 ". Because Java passes primitives by value, the original variables `a` and `b` in `main` remain unchanged. The `main` method then prints "4 8".

### Question 2

Consider the following code segment:

```java
public class LoopReturn {
    public static int processLoop(int limit) {
        int sum = 0;
        for (int k = 1; k <= limit; k++) {
            sum += k;
            if (sum >= 6) {
                return sum;
            }
        }
        return sum;
    }
    
    public static void main(String[] args) {
        System.out.print(processLoop(5));
    }
}
```

What is printed as a result of executing the `main` method?

- (A) The method halts early and returns 6 after three loop iterations
- (B) The method halts early and returns 3 after three loop iterations
- (C) The method completes all loops and returns 15 after five iterations
- (D) The method completes all loops and returns 10 after four iterations

**Answer: (A)** Tracing the loop step-by-step reveals the execution flow. On iteration 1 (`k=1`), `sum` becomes 1. On iteration 2 (`k=2`), `sum` becomes 3. On iteration 3 (`k=3`), `sum` becomes 6. The condition `sum >= 6` evaluates to true, triggering the `return` statement. The `return` keyword immediately halts method execution and outputs 6, preventing further iterations.

### Question 3

Consider the following class definition and `main` method:

```java
public class ScoreTracker {
    private int score = 10;
    
    public void updateScore(int score) {
        score = score + 5;
        this.score = this.score + score;
    }
    
    public void printScore() {
        System.out.print(score);
    }
    
    public static void main(String[] args) {
        ScoreTracker game = new ScoreTracker();
        game.updateScore(4);
        game.printScore();
    }
}
```

What is printed as a result of executing the `main` method?

- (A) The program will output the final integer value 19 to the console
- (B) The program will output the final integer value 30 to the console
- (C) The program will output the final integer value 10 to the console
- (D) The program will output the final integer value 25 to the console

**Answer: (A)** Tracing variable scope is required to determine the output. The instance variable `score` is initialized to 10. When `updateScore(4)` is called, the local parameter `score` shadows the instance variable. The statement `score = score + 5` updates the local parameter to 9. The statement `this.score = this.score + score` adds the instance variable (10) and the local parameter (9), updating the instance variable to 19.

### Question 4

The following method is intended to return the number of times the digit `d` appears in the positive integer `num`.

```java
public int countDigit(int num, int d) {
    int count = 0;
    while (num > 0) {
        /* missing code */
    }
    return count;
}
```

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

- (A) `if (num % 10 == d) { count++; } num /= 10;`
- (B) `if (num % 10 == d) { count++; } num %= 10;`
- (C) `if (num / 10 == d) { count++; } num /= 10;`
- (D) `if (num / 10 == d) { count++; } num %= 10;`

**Answer: (A)** Identifying the correct use of the modulo operator to isolate the last digit and integer division to reduce the number. The condition `num % 10 == d` correctly checks the rightmost digit, and `num /= 10` properly removes it for the next iteration.

### Question 5

The method `hasEvenDigit` is intended to return `true` if the positive integer `n` contains at least one even digit, and `false` otherwise.

```java
public boolean hasEvenDigit(int n) {
    while (n > 0) {
        int digit = n % 10;
        /* missing code */
        n /= 10;
    }
    return false;
}
```

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

- (A) `if (digit % 2 == 0) { return true; }`
- (B) `if (digit % 2 != 0) { return false; }`
- (C) `if (digit / 2 == 0) { return true; }`
- (D) `if (digit / 2 != 0) { return false; }`

**Answer: (A)** Recognizing that returning `true` immediately upon finding an even digit correctly satisfies the 'at least one' condition. The loop will exit early if an even digit is found, and only return `false` if all digits are checked and none are even.

### Question 6

A class contains a method intended to calculate and return the exact decimal average of two integer parameters, `a` and `b`. Which of the following method headers and return statements correctly implements this behavior?

- (A) `public double getAvg(int a, int b) { return (a + b) / 2.0; }`
- (B) `public double getAvg(int a, int b) { return (a + b) / 2; }`
- (C) `public double getAvg(int a, int b) { return a + b / 2.0; }`
- (D) `public int getAvg(int a, int b) { return (a + b) / 2; }`

**Answer: (A)** Applying proper type casting and operator precedence to achieve exact decimal division. Dividing the sum by the double literal `2.0` forces floating-point division, preventing the truncation of the decimal portion.

### Question 7

A library charges a late fee of $0.50 per day for the first 10 days a book is late, and $1.00 per day for each day after that. The method `calculateFee` is intended to return the total fee for a book that is `daysLate` days late.

```java
public double calculateFee(int daysLate) {
    if (daysLate <= 0) {
        return 0.0;
    }
    /* missing code */
}
```

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

- (A) `if (daysLate <= 10) return daysLate * 0.5; return 5.0 + (daysLate - 10) * 1.0;`
- (B) `if (daysLate <= 10) return daysLate * 0.5; return 5.0 + (daysLate) * 1.0;`
- (C) `if (daysLate <= 10) return daysLate * 0.5; return 0.0 + (daysLate) * 1.0;`
- (D) `if (daysLate >= 10) return daysLate * 0.5; return 5.0 + (daysLate - 10) * 1.0;`

**Answer: (A)** Translating a piecewise mathematical function into conditional logic. The code correctly calculates the base fee for the first 10 days and adds $1.00 only for the days exceeding the initial 10-day period.

### Question 8

The `Counter` class is intended to maintain a running total. The `addValue` method is intended to add the parameter `value` to the instance variable `value`.

```java
public class Counter {
    private int value;
    
    public void addValue(int value) {
        /* missing code */
    }
}
```

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

- (A) `this.value += value;`
- (B) `value += this.value;`
- (C) `value = value + value;`
- (D) `this.value += this.value;`

**Answer: (A)** Recognizing that the `this` keyword is required to access the instance variable when a parameter has the same name. The code correctly adds the parameter `value` to the instance variable `this.value`.

### Question 9

Consider the following class declaration.

```java
public class Book {
    private String author;
    private int pages;

    public Book(String a, int p) {
        author = a;
        pages = p;
    }

    public String getAuthor() {
        return author;
    }

    public static boolean isSameAuthor(Book b1, Book b2) {
        return b1.getAuthor().equals(b2.getAuthor());
    }
}
```

Which of the following best describes the behavior of the `isSameAuthor` method?

- (A) It is a class method that returns `true` if the two `Book` parameters have the exact same `author` instance variable value.
- (B) It is an instance method that returns `true` if the two `Book` parameters have the exact same `author` class variable value.
- (C) It is a class method that returns `true` if the two `Book` parameters have the exact same `pages` instance variable value.
- (D) It is an instance method that returns `true` if the two `Book` parameters have the exact same `pages` class variable value.

**Answer: (A)** Identifying method and variable types based on the `static` keyword. The `isSameAuthor` method is declared `static`, making it a class method, and it accesses the `author` instance variable of the passed objects via their getter methods.

### Question 10

Consider the following class declaration and code segment.

```java
public class Server {
    private static int activeConnections = 0;
    private static final int MAX_CONNECTIONS = 3;
    private boolean isConnected = false;

    public static boolean canConnect() {
        return activeConnections < MAX_CONNECTIONS;
    }

    public void connect() {
        if (canConnect() && !this.isConnected) {
            this.isConnected = true;
            activeConnections++;
        }
    }
}

/* 
 * The following code segment appears in a method in another class.
 */
Server s1 = new Server();
Server s2 = new Server();
Server s3 = new Server();
Server s4 = new Server();

s1.connect();
s2.connect();
s3.connect();
s4.connect();
s1.connect();
```

Which of the following best describes the state of the objects after the code segment executes?

- (A) Three `Server` objects successfully connect, one fails to connect, and the class variable `activeConnections` evaluates to 3.
- (B) Four `Server` objects successfully connect, none fail to connect, and the class variable `activeConnections` evaluates to 4.
- (C) Three `Server` objects successfully connect, one fails to connect, and the instance variable `activeConnections` evaluates to 3.
- (D) Four `Server` objects successfully connect, none fail to connect, and the instance variable `activeConnections` evaluates to 4.

**Answer: (A)** Tracing the execution of instance methods that interact with shared class variables. Because `activeConnections` is `static`, it is shared across all `Server` instances. The first three calls to `connect()` increment the shared counter to 3. The fourth call fails because `canConnect()` returns false, leaving three connected servers and the class variable at 3.

## Review the Unit

- [3.1 Abstraction and Program Design](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz)
- [3.2 Impact of Program Design](/ap-comp-sci-a/unit-3/impact-of-program-design/study-guide/BtQMRqn2Eh4i4PO0GRd9)
- [3.3 Anatomy of a Class](/ap-comp-sci-a/unit-3/anatomy-of-a-class/study-guide/DcGY5KOyK98H9Fn2w8jh)
- [3.4 Constructors](/ap-comp-sci-a/unit-3/constructors/study-guide/3Ez6zzak2wRwMrTj2ZQk)
- [3.5 Writing Methods](/ap-comp-sci-a/unit-3/writing-methods/study-guide/rtuMpRFmidkpYTzvDndS)
- [3.6 Accessor Methods](/ap-comp-sci-a/unit-3/accessor-methods/study-guide/aGdfIlIw7aOvNseJG5R1)
- [3.7 Static Variables and Methods](/ap-comp-sci-a/unit-3/static-variables-and-methods/study-guide/zzhHVbXBRCZQ7ng3EeWX)
- [3.8 Scope and Access](/ap-comp-sci-a/unit-3/scope-and-access/study-guide/56FUK4RSofr7slzwm6xm)
- [3.9 This Keyword](/ap-comp-sci-a/unit-3/this-keyword/study-guide/Zste3M7m756uzwR0zCQK)

## Other Unit Tests

- [Unit 1: Using Objects and Methods](/ap-comp-sci-a/unit-test/1)
- [Unit 2: Selection and Iteration](/ap-comp-sci-a/unit-test/2)
- [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`)
