Skip to main content

AP Computer Science A Unit 1 practice questions

Try 10 multiple-choice questions from Unit 1 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

    Consider the following code segment.

    </>Java
    int a = 11;
    int b = 4;
    double x = (double) (a / b);
    double y = (double) a / b;
    System.out.println(x + " " + y);

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

    2.0 2.75

    2.75 2.75

    2.0 2.00

    2.75 2.0

    Show answer

    Answer: (A)

    Tracing the order of operations for casting and division. The expression (a / b) performs integer division first, yielding 2, which is then cast to 2.0. The expression (double) a casts 11 to 11.0 first, resulting in floating-point division and 2.75.

  2. Question 2

    Consider the following code segment.

    </>Java
    double num = -5.8;
    int a = (int) num;
    int b = (int) (num - 0.5);
    System.out.println(a + " " + b);

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

    -5 -6

    -6 -5

    -6 -6

    -5 -5

    Show answer

    Answer: (A)

    Evaluating the truncation behavior of casting negative floating-point numbers. Casting -5.8 directly to an integer truncates the decimal, resulting in -5. Subtracting 0.5 yields -6.3, which truncates to -6.

  3. Question 3

    Consider the following code segment.

    </>Java
    int x = 10;
    int y = 3;
    x %= y;
    y += x;
    x *= y;
    y /= 2;
    System.out.println(x + " " + y);

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

    4 2

    4 1

    3 2

    3 1

    Show answer

    Answer: (A)

    Tracing the sequential execution of compound assignment operators requires tracking the updated state of each variable. The variable x becomes 1 (10 % 3), then y becomes 4 (3 + 1). Next, x becomes 4 (1 * 4), and finally y becomes 2 (4 / 2), resulting in the output of 4 and 2.

  4. Question 4

    Consider the following code segment.

    </>Java
    String s = "Val: ";
    int n = 5;
    n += 2;
    s += n;
    n *= 2;
    s += n;
    System.out.println(s);

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

    "Val: 714"

    "Val: 510"

    "Val: 721"

    "Val: 710"

    Show answer

    Answer: (A)

    Evaluating compound assignment operators with mixed data types requires tracking both numeric operations and string concatenations. The integer n is updated to 7 and concatenated to form "Val: 7", then n is multiplied by 2 to become 14 and concatenated again to form "Val: 714".

  5. Question 5

    Consider the following code segment.

    </>Java
    int val = 10;
    int mod = 3;
    val *= mod + 2;
    val -= mod;
    System.out.println(val);

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

    47

    29

    50

    32

    Show answer

    Answer: (A)

    Applying the correct order of operations to compound assignment statements requires evaluating the entire right-hand expression first. The expression mod + 2 evaluates to 5, so val *= 5 updates val to 50. The final statement val -= 3 reduces it to 47.

  6. Question 6

    Consider the following code segment.

    </>Java
    int a = 2;
    int b = 3;
    String s1 = a + b + " sum";
    String s2 = "sum " + a + b;
    System.out.println(s1 + ", " + s2);

    What is the result of attempting to compile and execute this code segment?

    The code fails to compile due to a type mismatch.

    The code compiles and prints out 5 sum, sum 5.

    The code compiles and prints out 5 sum, sum 23.

    The code compiles and prints out 23 sum, sum 23.

    Show answer

    Answer: (C)

    Applying Java's rules for operator precedence and String concatenation. In s1, the integer addition a + b evaluates first to 5, which is then concatenated with the String. In s2, the operation evaluates left-to-right, concatenating the String with a to form "sum 2", which is then concatenated with b to form "sum 23".

  7. Question 7

    Consider the following class definition and code segment.

    </>Java
    public class Player {
        private int score;
        private int level;
    
        public Player(int s, int l) {
            score = s;
            level = l;
        }
    
        public void update(int points) {
            score += points;
            level++;
            score *= level;
        }
    
        public int getScore() {
            return score;
        }
    }
    </>Java
    // Code segment
    Player p1 = new Player(10, 1);
    p1.update(5);
    p1.update(2);
    System.out.println(p1.getScore());

    What is printed when the code segment is executed?

    96

    64

    34

    26

    Show answer

    Answer: (A)

    Tracing the state of an object across multiple method calls involving compound assignment and increment operators. In the first call, score increases by 5 (to 15), level increments to 2, and score is multiplied by 2 (to 30). In the second call, score increases by 2 (to 32), level increments to 3, and score is multiplied by 3 (to 96).

  8. Question 8

    A programmer is designing a Thermostat class to track the current room temperature, whether the heating system is actively running, and the total number of days since the air filter was last replaced. Which of the following sets of instance variables represents the most appropriate design for this class?

    private double roomTemp; private boolean isHeating; private int filterDays;

    private int roomTemp; private boolean isHeating; private int filterDays;

    private double roomTemp; private String isHeating; private int filterDays;

    private double roomTemp; private boolean isHeating; private double filterDays;

    Show answer

    Answer: (A)

    Evaluating data requirements for physical measurements and states. Temperature requires a double for decimal precision, heating state requires a boolean for true/false status, and days require an int for whole numbers.

  9. Question 9

    A programmer is designing a method to process loan applications. The method must accept the applicant's credit score (a whole number from 300 to 850) and their exact annual income, then return a value indicating whether the loan is approved. Which method signature is most appropriate?

    public boolean processLoan(int creditScore, double annualIncome)

    public String processLoan(int creditScore, double annualIncome)

    public boolean processLoan(double creditScore, double annualIncome)

    public boolean processLoan(int creditScore, int applicantIncome)

    Show answer

    Answer: (A)

    Analyzing the required inputs and outputs for a financial calculation. The approval status requires a boolean return type, the credit score requires an int, and the exact income requires a double.

  10. Question 10

    A programmer is designing a Book class for an online store. The class must store the book's title, the total number of pages, and the retail price. Which of the following sets of instance variables represents the most appropriate design?

    private String bookTitle; private int totalPages; private double itemPrice;

    private String bookTitle; private double totalPages; private double itemPrice;

    private String bookTitle; private int totalPages; private int itemPrice;

    private char bookTitle; private int totalPages; private double itemPrice;

    Show answer

    Answer: (A)

    Selecting appropriate primitive and reference types for distinct data categories. The title requires a String reference type, pages require an int for whole numbers, and price requires a double for decimals.

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

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

  • 20 Multiple Choice Questions (43 minutes)

Questions are drawn from 938 multiple-choice questions written for Unit 1, Using Objects and Methods.

Review Unit 1 before you start

Other AP Computer Science A unit tests