Compound Boolean Expressions

AP Computer Science A· difficulty 4/5

public static int run() {
    int x = 5;
    if (x > 0 && x++ < 10 || x++ > 100) {
        x += 1;
    }
    return x;
}
// Call: System.out.println(run());

What is printed?

  • A

    7

    check_circle
  • B

    8

  • C

    5

  • D

    6

Explanation

x>0 true. x++ <10: post-inc returns 5 (true), x becomes 6. && both true. || short-circuits (no second x++). Then x+=1 makes x=7.

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

Related questions

AP Computer Science A · Compound Boolean Expressions Practice Question | Acemy