AP Computer Science A · Topic 1.3

Expressions and Assignment Statements Practice

Part of Primitive Types.

Practice questions

29

Want a predicted score for the whole AP CSA exam? Take the 20-question diagnostic and Lumi will plan the rest.

Sample questions

5 of 29 — sign in to practice the rest with adaptive difficulty and mastery tracking.

  1. Sample 1difficulty 2/5

    int x = 7;
    int y = 3;
    System.out.println(x / y);

    What is printed?

    • A

      2

      check_circle
    • B

      2.33

    • C

      2.0

    • D

      2.333333

    Why

    When both operands are int, Java performs integer division and truncates toward zero, so 7 / 3 evaluates to 2.

  2. Sample 2difficulty 2/5

    int a = 17;
    int b = 5;
    System.out.println(a % b);

    What is printed?

    • A

      2

      check_circle
    • B

      3

    • C

      3.4

    • D

      0

    Why

    17 % 5 returns the remainder of 17 / 5, which is 2 (since 5 * 3 = 15 and 17 - 15 = 2).

  3. Sample 3difficulty 2/5

    public static double half(int n) {
        return n / 2;
    }

    What is wrong with this method?

    • A

      Division by 2 is undefined for negatives

    • B

      Integer division truncates; cast at least one operand to double

      check_circle
    • C

      The return type should be int

    • D

      Nothing is wrong

    Why

    n / 2 performs integer division because both operands are int, so half(3) returns 1.0. Use (double) n / 2 or n / 2.0 to retain the fractional part.

  4. Sample 4difficulty 2/5

    <code>-3 + 2 * -2</code> evaluates to

    • A

      −1

    • B

      1

    • C

      −7

      check_circle
    • D

      −10

    Why

    2 * −2 = −4; −3 + (−4) = −7.

  5. Sample 5difficulty 2/5

    int result = (2 + 3) * 4;
    System.out.println(result);

    What is printed?

    • A

      20

      check_circle
    • B

      14

    • C

      24

    • D

      9

    Why

    Parentheses force 2 + 3 = 5 to be evaluated first, then 5 * 4 = 20.