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

  2. Question 2

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

  3. Question 3

    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?

    29

    32

    47

    50

    Show answer

    Answer: (C)

    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.

  4. Question 4

    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.

  5. Question 5

    A programmer is designing a method to determine if a package exceeds a maximum weight limit. The method receives the package's exact weight in kilograms and the maximum allowed weight in kilograms. Which method signature is most appropriate?

    public boolean checkWeight(double packageWt, double maxWt)

    public String checkWeight(double packageWt, double maxWt)

    public boolean checkWeight(int packageWt, double maxWt)

    public boolean checkWeight(double packageWt, int maxWt)

    Show answer

    Answer: (A)

    Evaluating data types for continuous measurements and logical outcomes. Exact weights require double parameters for decimal precision, and the yes/no outcome requires a boolean return type.

  6. Question 6

    Consider the following class declaration.

    </>Java
    public class Logger {
        public void logError(String msg, int code) {
            /* missing code */
        }
    }

    A program calls logError("Timeout", 404) followed by logError("Not found", 500). The intended output is:

    </>TEXT
    Error 404: Timeout
    Error 500: Not found

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

    System.out.print("Error " + code + ": "); System.out.println(msg);

    System.out.println("Error " + code + ": "); System.out.print(msg);

    System.out.print("Error " + code + ": "); System.out.print(msg);

    System.out.println("Error " + code + ": "); System.out.println(msg);

    Show answer

    Answer: (A)

    Recognizing that System.out.print keeps the cursor on the same line, while System.out.println moves it to the next line after printing. To achieve the format Error 404: Timeout on a single line, the prefix must be printed with print, and the message with println to ensure subsequent logs start on a new line.

  7. Question 7

    Consider the following class declaration.

    </>Java
    public class TimeFormatter {
        public void printDelimiter() {
            System.out.print(":");
        }
        
        public void formatTime(int h, int m, int s) {
            /* missing code */
        }
    }

    The formatTime method is intended to print the time in the format h:m:s and then move the cursor to a new line. Which of the following can replace /* missing code */ so the method works as intended?

    System.out.print(h); printDelimiter(); System.out.print(m); printDelimiter(); System.out.print(s);

    System.out.println(h); printDelimiter(); System.out.println(m); printDelimiter(); System.out.println(s);

    System.out.print(h); printDelimiter(); System.out.print(m); printDelimiter(); System.out.println(s);

    System.out.println(h); printDelimiter(); System.out.print(m); printDelimiter(); System.out.print(s);

    Show answer

    Answer: (C)

    To print the entire time on one line and then move to the next line, the hours, minutes, and delimiters must be printed using print. Using println for the final value s prints the seconds on the same line and then moves the cursor to a new line.

  8. Question 8

    Consider the following code segment:

    </>Java
    Scanner scan = new Scanner(inputString);
    String first = scan.next();
    int id = scan.nextInt();

    For the variable first to be assigned the value "User" and the variable id to be assigned the value 42, which of the following must be the initial condition of inputString?

    String inputString = "User 42 ";

    String inputString = "User42 ";

    String inputString = "User,42 ";

    String inputString = "User 4.2";

    Show answer

    Answer: (A)

    Tracing the Scanner methods reveals that next() reads characters until it encounters whitespace, successfully assigning "User" to first. The subsequent nextInt() call skips the space and reads the integer 42, assigning it to id.

  9. Question 9

    Consider the following code segment:

    </>Java
    Scanner s = new Scanner(data);
    double val1 = s.nextDouble();
    int val2 = s.nextInt();

    Which initialization of the String data allows the code segment to execute completely without throwing an InputMismatchException?

    String data = "4.5 20 ";

    String data = "20 4.5 ";

    String data = "4.5, 20";

    String data = "4.5. 20";

    Show answer

    Answer: (A)

    Analyzing the sequence of method calls shows that nextDouble() correctly parses "4.5" into a double. The Scanner then advances past the space, and nextInt() successfully parses "20" into an integer.

  10. Question 10

    Consider the following code segment:

    </>Java
    String text = "100 200";
    /* missing code */
    int sum = scan.nextInt() + scan.nextInt();

    Which replacement for /* missing code */ properly establishes the initial condition needed for the Scanner to read from the text variable?

    Scanner scan = new Scanner(text);

    Scanner scan = new Scanner("100");

    Scanner scan = new Scanner("200");

    Scanner scan = new Scanner("text");

    Show answer

    Answer: (A)

    Recognizing object instantiation requirements, this correctly passes the String variable text into the Scanner constructor, linking the input stream to the string containing "100 200".

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 924 multiple-choice questions written for Unit 1, Using Objects and Methods.

Review Unit 1 before you start

Other AP Computer Science A unit tests