Writing Methods

AP Computer Science A· difficulty 2/5

public static int isPositive(int n) {
    return n > 0;
}

What is wrong?

  • A

    The method must be void

  • B

    Nothing is wrong

  • C

    Return type must be boolean to match the expression n > 0

    check_circle
  • D

    n must be checked with .equals

Explanation

n > 0 yields a boolean; an int return type produces a compile error. Change the type to boolean.

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

Related questions