Writing Methods

AP Computer Science A· difficulty 3/5

public class T {
    public int f(int n) {
        if (n > 0) return 1;
    }
}
// ...

What is the result?

  • A

    Returns 1

  • B

    Returns 0 by default

  • C

    Compile-time error: missing return statement

    check_circle
  • D

    Run-time error

Explanation

All paths in a non-void method must return a value; the compiler reports a missing return.

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

Related questions