Casting and Ranges of Variables

AP Computer Science A· difficulty 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

Explanation

<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.

Want 10 more like this — adaptive to your weak spots?

Related questions