Writing Methods

AP Computer Science A· difficulty 2/5

public static int max(int a, int b) {
    if (a > b) {
        return a;
    }
    // line 4
}

What must be added at line 4 to make the method compile and work?

  • A

    return b;

    check_circle
  • B

    Nothing; the code compiles as is

  • C

    break;

  • D

    return 0;

Explanation

Java requires every code path of a non-void method to return a value. When a <= b, no return executes, so the method must return b.

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

Related questions