AP Computer Science A · Topic 1.5

Casting and Ranges of Variables Practice

Part of Primitive Types.

Practice questions

24

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

  1. Sample 1difficulty 3/5

    Which expression yields <code>2.5</code> when <code>int a = 5;</code>?

    • A

      a / 2.0 / 2

    • B

      (double) (a / 2)

    • C

      a / 2

    • D

      (double) a / 2

      check_circle

    Why

    <code>(double) a / 2</code> casts first, then divides → 5.0 / 2 = 2.5. <code>(double)(a/2)</code> casts after the int division → 2.0.

  2. Sample 2difficulty 3/5

    int a = 9;
    int b = 4;
    double r = (double) a / b;
    System.out.println(r);

    What is printed?

    • A

      2

    • B

      2.0

    • C

      2.25

      check_circle
    • D

      2.5

    Why

    Casting a to double before division forces double division, so 9.0 / 4 = 2.25.

  3. Sample 3difficulty 3/5

    <code>int a = 7; double b = a / 2;</code><code>b</code> equals

    • A

      3

    • B

      3.0

      check_circle
    • C

      Compile error

    • D

      3.5

    Why

    Right side first: int division a/2 = 3; assigned to double → 3.0.

  4. Sample 4difficulty 3/5

    What is the value of <code>7.0 / 2</code>?

    • A

      3.5

      check_circle
    • B

      3

    • C

      Compiler error

    • D

      4

    Why

    A <code>double</code> operand promotes the other to <code>double</code>; 7.0 / 2 = 3.5.

  5. Sample 5difficulty 3/5

    int a = 9;
    int b = 4;
    double r = (double)(a / b);
    System.out.println(r);

    What is printed?

    • A

      2.0

      check_circle
    • B

      2.25

    • C

      2

    • D

      2.5

    Why

    The cast applies after the int division a / b which is 2; converting 2 to double gives 2.0.