Writing Methods

AP Computer Science A· difficulty 2/5

public class Tester {
    public int sign(int n) {
        if (n > 0) return 1;
        if (n < 0) return -1;
        return 0;
    }
}
// ...
Tester t = new Tester();
System.out.println(t.sign(0));

What is printed?

  • A

    Compile-time error

  • B

    -1

  • C

    1

  • D

    0

    check_circle

Explanation

n is 0, so neither if branch returns; the final return 0 executes.

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

Related questions