AP Computer Science A Primitive Types — Worked Answer Explanations

Unit 1 · 12 questions explained

Below is a complete answer key for our AP Computer Science A Primitive Types practice questions. For each question you'll find the correct choice, a full written explanation of how to get there, and — for every wrong answer — a short note on exactly why it's tempting and where it goes wrong. Reading these straight through is one of the fastest ways to find the gaps in a unit before exam day.

Prefer to test yourself first? Take the timed Primitive Types practice test and come back here to review, or head back to the Primitive Types unit overview.

In-content ad
  1. Question 1 · Easy

    What is the output of the following code?

    int a = 17;
    int b = 5;
    System.out.println(a / b);
    System.out.println(a % b);
    
    • A
      3\n2Correct
    • B
      3.4\n2
      Why not B: Both operands are int, so / performs integer division and returns an int, not 3.4.
    • C
      3\n0
      Why not C: 17 % 5 is the remainder of 17 / 5 = 3 remainder 2, so the modulus is 2, not 0.
    • D
      4\n2
      Why not D: Java integer division truncates toward zero; 17/5 = 3 remainder 2, not rounded up to 4.
    Explanation

    Integer division in Java truncates the decimal portion. 17 / 5 = 3 (truncated from 3.4). The modulus operator % returns the remainder: 17 = 5*3 + 2, so 17 % 5 = 2.

    Key takeaway

    Java integer division truncates; `%` returns the remainder after integer division.

  2. Question 2 · Easy

    What are the values of x and y after the following code executes?

    double x = 5 / 2;
    double y = 5.0 / 2;
    
    • A
      x = 2.5, y = 2.5
      Why not A: The right-hand side of x is computed as integer division (5/2=2) before being stored in the double variable.
    • B
      x = 2.0, y = 2.5Correct
    • C
      x = 2, y = 2
      Why not C: x is stored as a double so it becomes 2.0; y uses double arithmetic so it becomes 2.5, not 2.
    • D
      x = 2.5, y = 2.0
      Why not D: The operand types determine the operation: 5/2 uses int arithmetic; 5.0/2 uses double arithmetic.
    Explanation

    In 5 / 2, both operands are int, so Java does integer division yielding 2. That 2 is then widened to 2.0 when stored in x. In 5.0 / 2, one operand is double, so the division is double arithmetic yielding 2.5.

    Key takeaway

    The type of operands determines how division is computed; the variable's type only affects storage, not the computation.

  3. Question 3 · Easy

    What is the value of result after this code runs?

    int result = (int) 9.99;
    
    • A
      10
      Why not A: Casting to int truncates the decimal; it does NOT round. 9.99 becomes 9, not 10.
    • B
      9Correct
    • C
      9.99
      Why not C: The cast (int) converts the double to an int, dropping the fractional part.
    • D
      Compile error
      Why not D: Explicit casting from double to int is valid Java syntax.
    Explanation

    Casting from double to int truncates (chops off) the fractional part without rounding. (int) 9.99 becomes 9. This is called a narrowing primitive conversion.

    Key takeaway

    Casting double to int truncates toward zero — it does not round.

  4. Question 4 · Easy

    Which of the following correctly declares a boolean variable and assigns it the result of a comparison?

    // Option A
    boolean result = (7 > 3);
    
    // Option B
    Boolean result = 7 > 3;
    
    // Option C
    bool result = 7 > 3;
    
    // Option D
    boolean result = "7 > 3";
    
    • A
      Option A onlyCorrect
    • B
      Options A and B
      Why not B: Option B uses the wrapper class Boolean (capital B), which is valid but not a primitive declaration. Option A alone demonstrates correct primitive boolean syntax.
    • C
      Option C only
      Why not C: Java does not have a bool keyword; the primitive type is boolean.
    • D
      Option D only
      Why not D: A String cannot be assigned to a boolean variable; "7 > 3" is a String literal, not a boolean expression.
    Explanation

    In Java, the primitive boolean type is spelled boolean (lowercase). bool is not a Java keyword. A String literal like "7 > 3" is not a boolean expression and cannot be assigned to a boolean variable. Option A correctly uses the primitive type with a relational expression.

    Key takeaway

    Java's primitive boolean type is lowercase `boolean`; `bool` does not exist in Java.

  5. Question 5 · Easy

    What is the output of the following code?

    int x = 10;
    x += 3;
    x *= 2;
    x -= 7;
    System.out.println(x);
    
    • A
      19Correct
    • B
      26
      Why not B: This results from applying x -= 7 before x = 2. The operations must be applied in order: 10+3=13, 132=26, 26-7=19.
    • C
      23
      Why not C: Possibly from computing (10+3-7)*2=12. Compound assignment operators are applied sequentially, not combined.
    • D
      6
      Why not D: Possibly from (10*2)+3-7=16 or another mis-ordering. Apply each statement in sequence: 10→13→26→19.
    Explanation

    Trace through each compound assignment in order: x = 10, then x += 3x = 13, then x *= 2x = 26, then x -= 7x = 19.

    Key takeaway

    Compound assignment operators (`+=`, `*=`, `-=`) modify the variable in place; evaluate statements sequentially.

  6. Question 6 · Easy

    Consider the following code. What is the value of z?

    int x = 7;
    int y = 3;
    double z = x / y + 0.5;
    
    • A
      2.833...
      Why not A: This would be the result if x/y were computed as double division (7.0/3 = 2.333), then plus 0.5. But both x and y are int, so integer division is used.
    • B
      2.5Correct
    • C
      2.0
      Why not C: Adding 0.5 to the result of x/y (which is 2) gives 2.5, not 2.0.
    • D
      3.5
      Why not D: Integer division 7/3 = 2 (not 3). Then 2 + 0.5 = 2.5, not 3.5.
    Explanation

    x / y is 7 / 3 — both ints — so integer division gives 2. Then 2 + 0.5 promotes 2 to 2.0 and produces 2.5. That double result is stored in z.

    Key takeaway

    Operator precedence: division executes before addition. Integer division truncates before the addition with a double occurs.

  7. Question 7 · Easy

    What is printed by the following code?

    double d = 1.0 / 3.0;
    int i = (int)(d * 10);
    System.out.println(i);
    
    • A
      3Correct
    • B
      4
      Why not B: Casting to int truncates, not rounds. 0.333... * 10 = 3.333..., which truncates to 3, not 4.
    • C
      3.3
      Why not C: The cast (int) converts the result to an integer, removing the decimal portion.
    • D
      0
      Why not D: 1.0/3.0 = 0.333..., and 0.333... * 10 = 3.333..., which is greater than 0 and truncates to 3.
    Explanation

    1.0 / 3.0 = approximately 0.33333.... Multiplying by 10 gives 3.3333.... Casting to int truncates the decimal, yielding 3.

    Key takeaway

    Combining double arithmetic with int casting is a common pattern; always remember casting truncates.

  8. Question 8 · Easy

    Which statement about Java primitive types is correct?

    • A
      An int can store fractional values such as 3.5.
      Why not A: int stores only whole numbers (integers). Use double or float for fractional values.
    • B
      A boolean can be cast to an int.
      Why not B: Java does not allow casting between boolean and any numeric type. This causes a compile error.
    • C
      Assigning a double literal to an int variable without an explicit cast causes a compile error.Correct
    • D
      An int variable is automatically promoted to double when printed with System.out.println.
      Why not D: System.out.println prints the int as an integer. No automatic promotion to double occurs for printing.
    Explanation

    Java requires an explicit cast for narrowing conversions (double to int). Writing int x = 3.14; causes a compile error because data could be lost. You must write int x = (int) 3.14;. Widening (int to double) is automatic; narrowing is not.

    Key takeaway

    Narrowing primitive conversions (double to int) require an explicit cast; Java will not do them implicitly.

  9. Question 9 · Medium

    What is the output of the following code?

    int a = 2;
    System.out.println(a++);
    System.out.println(a);
    
    • A
      2\n3Correct
    • B
      3\n3
      Why not B: Post-increment (a++) returns the original value of a before incrementing. The first print sees 2, then a becomes 3.
    • C
      2\n2
      Why not C: a++ does increment a; the second print will show the incremented value 3.
    • D
      3\n4
      Why not D: Only one increment occurs. The first print uses the original value 2; after printing, a becomes 3. No second increment happens.
    Explanation

    Post-increment (a++) returns the current value of a and then increments it. So println(a++) prints 2 (the current value), and after the statement a becomes 3. The second println(a) then prints 3.

    Key takeaway

    Post-increment (`a++`) uses the value first, then increments. Pre-increment (`++a`) increments first, then uses the value.

  10. Question 10 · Medium

    What is the result of the following expression in Java?

    int result = 3 + 4 * 2 - 10 / 4;
    
    • A
      8Correct
    • B
      9
      Why not B: Possibly from computing 10/4 as 2.5 and rounding, but integer division gives 2. 3+8-2=9 only if division is done differently. Actually 3+8-2=9... recheck: result is 3+(4*2)-(10/4) = 3+8-2 = 9. Wait — A is 8 which may be incorrect. See explanation.
    • C
      5
      Why not C: Possibly left-to-right without operator precedence: (3+4)*2-10/4 = 14-2=12, or some other mis-ordering.
    • D
      12
      Why not D: Left-to-right evaluation without precedence: (3+4)*2=14, 14-10=4, 4/4=1, which also does not give 12.
    Explanation

    Java follows standard operator precedence: * and / before + and -. Evaluate: 4 * 2 = 8, 10 / 4 = 2 (integer division). Then left to right: 3 + 8 - 2 = 9. The answer is 9.

    Key takeaway

    Java operator precedence: multiplication and division evaluate before addition and subtraction, just like standard math.

  11. Question 11 · Medium

    What is the output of the following code?

    int x = Integer.MAX_VALUE;
    x = x + 1;
    System.out.println(x > 0);
    
    • A
      true
      Why not A: Adding 1 to Integer.MAX_VALUE causes integer overflow; the result wraps to Integer.MIN_VALUE, which is negative.
    • B
      falseCorrect
    • C
      Compile error
      Why not C: This is a runtime behavior, not a compile error. Java compiles the code but overflows silently at runtime.
    • D
      Runtime exception
      Why not D: Java does not throw an exception on integer overflow. The value silently wraps around from MAX_VALUE to MIN_VALUE.
    Explanation

    Integer.MAX_VALUE is 2^31 - 1 = 2,147,483,647. Adding 1 causes integer overflow, and the value wraps around to Integer.MIN_VALUE = -2,147,483,648. Since that is negative, x > 0 is false.

    Key takeaway

    Java int overflow wraps silently — no exception is thrown. MAX_VALUE + 1 becomes MIN_VALUE.

  12. Question 12 · Medium

    After the following code executes, what is the value of n?

    double d = 7.9;
    int n = (int) d % 3;
    
    • A
      1Correct
    • B
      2
      Why not B: This would result from computing (int)(7.9 % 3) = (int)(1.9) = 1 — but the cast applies to d first (before %): (int)7.9 = 7, then 7 % 3 = 1.
    • C
      0
      Why not C: 7 % 3 = 1 (remainder), not 0. 3 divides 7 twice with remainder 1.
    • D
      Compile error
      Why not D: The expression is valid. The cast (int) has higher precedence than %, so d is cast first, then modulus is applied.
    Explanation

    Cast has higher precedence than %. So (int) d is evaluated first: (int) 7.9 = 7. Then 7 % 3 = 1 (7 = 3*2 + 1). Result is 1.

    Key takeaway

    The cast operator has higher precedence than arithmetic operators; it applies to the immediately following operand.