AP Computer Science A · Topic 2.9

Using the Math Class Practice

Part of Using Objects.

Practice questions

18

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

  1. Sample 1difficulty 2/5

    int x = Math.abs(-7);
    System.out.println(x);

    What is printed?

    • A

      7

      check_circle
    • B

      -7

    • C

      0

    • D

      1

    Why

    Math.abs returns the absolute value of its argument, so Math.abs(-7) returns 7.

  2. Sample 2difficulty 2/5

    <code>Math.abs(-7)</code> returns

    • A

      0

    • B

      −7

    • C

      1

    • D

      7

      check_circle

    Why

    Absolute value: |−7| = 7.

  3. Sample 3difficulty 2/5

    double r = Math.sqrt(16);
    System.out.println(r);

    What is printed?

    • A

      4

    • B

      4.0

      check_circle
    • C

      16.0

    • D

      2.0

    Why

    Math.sqrt returns a double; the square root of 16 is 4.0.

  4. Sample 4difficulty 2/5

    int m = Math.max(-4, -9);
    System.out.println(m);

    What is printed?

    • A

      9

    • B

      -4

      check_circle
    • C

      -9

    • D

      4

    Why

    Math.max returns the greater of its arguments; -4 > -9, so the result is -4.

  5. Sample 5difficulty 2/5

    double d = Math.abs(-3.5);
    System.out.println(d);

    What is printed?

    • A

      3.5

      check_circle
    • B

      -3.5

    • C

      3

    • D

      4

    Why

    Math.abs(double) returns the absolute value as a double, so -3.5 becomes 3.5.