Compound Boolean Expressions

AP Computer Science A· difficulty 4/5

public static int run() {
    int[] a = {1, 2, 3};
    int i = 5;
    if (i < a.length && a[i] > 0) return 1;
    if (i >= a.length || a[i] > 0) return 2;
    return 3;
}
// Call: System.out.println(run());

What is printed?

  • A

    ArrayIndexOutOfBoundsException

  • B

    3

  • C

    1

  • D

    2

    check_circle

Explanation

First if: i<a.length false, short-circuits, no return. Second if: i>=a.length true, short-circuits, returns 2.

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

Related questions