Casting and Ranges of Variables

AP Computer Science A· difficulty 3/5

public static double run() {
    int a = 5;
    double b = 2.0;
    return (int)(a / b) + a / 2;
}
// Call: System.out.println(run());

What is printed?

  • A

    4.5

  • B

    2.5

  • C

    4.0

    check_circle
  • D

    5.0

Explanation

a/b = 5/2.0 = 2.5; (int) cast = 2. a/2 (int) = 2. Sum = 4 stored as double = 4.0.

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

Related questions