Boolean Expressions

AP Computer Science A· difficulty 2/5

public static boolean allPositive(int[] a) {
    /* missing */
    for (int x : a) {
        if (x <= 0) return false;
    }
    return true;
}

Which initialization, if any, is needed for /* missing */?

  • A

    return true;

  • B

    boolean result = true;

  • C

    int count = 0;

  • D

    Nothing is needed; the line can be removed

    check_circle

Explanation

The method correctly returns false on first non-positive and true otherwise; no extra variable is needed before the loop.

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

Related questions