Casting and Ranges of Variables

AP Computer Science A· difficulty 3/5

public static int classify(int n) {
    if (n < 0) return -1;
    else if (n < 10) return 0;
    else if (n < 100) return 1;
    else return 2;
}
public static int run() {
    return classify(-5) + classify(50) + classify(100) + classify(5);
}
// Call: System.out.println(run());

What is printed?

  • A

    0

  • B

    1

  • C

    3

  • D

    2

    check_circle

Explanation

classify gives -1, 1, 2, 0. Sum = -1+1+2+0 = 2.

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

Related questions