AP Computer Science A · Topic 1.4

Compound Assignment Operators Practice

Part of Primitive Types.

Practice questions

5

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 5 — sign in to practice the rest with adaptive difficulty and mastery tracking.

  1. Sample 1difficulty 2/5

    x = x + 5;

    Which statement is equivalent?

    • A

      x++ 5;

    • B

      ++x = 5;

    • C

      x += 5;

      check_circle
    • D

      x =+ 5;

    Why

    The compound assignment x += 5 is shorthand for x = x + 5.

  2. Sample 2difficulty 2/5

    int n = 20;
    n /= 6;
    System.out.println(n);

    What is printed?

    • A

      3

      check_circle
    • B

      3.33

    • C

      2

    • D

      4

    Why

    n /= 6 sets n to n / 6 with integer division, so 20 / 6 = 3.

  3. Sample 3difficulty 2/5

    int x = 10;
    x += 3;
    x *= 2;
    System.out.println(x);

    What is printed?

    • A

      26

      check_circle
    • B

      20

    • C

      23

    • D

      16

    Why

    x += 3 makes x equal to 13, then x *= 2 makes x equal to 26.

  4. Sample 4difficulty 2/5

    Given <code>int x = 10;</code>, what is <code>x</code> after <code>x += 5;</code>?

    • A

      10

    • B

      15

      check_circle
    • C

      5

    • D

      50

    Why

    <code>x += 5;</code> is equivalent to <code>x = x + 5;</code>.

  5. Sample 5difficulty 3/5

    int n = 23;
    n %= 6;
    System.out.println(n);

    What is printed?

    • A

      0

    • B

      3

    • C

      4

    • D

      5

      check_circle

    Why

    n %= 6 sets n equal to n % 6; 23 % 6 = 5.