Casting and Ranges of Variables

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

Explanation

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

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

Related questions