Casting and Ranges of Variables

AP Computer Science A· difficulty 4/5

public static double run() {
    double r = 0;
    for (int i = 1; i <= 4; i++) {
        r += 1.0 / i;
    }
    return Math.round(r * 100) / 100.0;
}
// Call: System.out.println(run());

What is printed?

  • A

    2.5

  • B

    2.0

  • C

    1.92

  • D

    2.08

    check_circle

Explanation

1 + 0.5 + 0.333... + 0.25 = 2.0833. Round to 2 decimals = 2.08.

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

Related questions