Compound Boolean Expressions

AP Computer Science A· difficulty 3/5

public static int sc() {
    int x = 5;
    int y = 10;
    if (x > 3 || ++y > 20) {
        x++;
    }
    return x + y;
}
// Call: System.out.println(sc());

What is printed?

  • A

    16

    check_circle
  • B

    26

  • C

    15

  • D

    17

Explanation

x>3 is true so ++y is short-circuited (y stays 10). x++ makes x=6. Return 6 + 10 = 16.

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

Related questions