Compound Boolean Expressions

AP Computer Science A· difficulty 4/5

public static int run() {
    int x = 1;
    int y = 1;
    if (++x == 2 || ++y == 2) {
        x = x + y;
    }
    return x * 10 + y;
}
// Call: System.out.println(run());

What is printed?

  • A

    42

  • B

    31

    check_circle
  • C

    32

  • D

    41

Explanation

++x makes x=2; comparison true. || short-circuits so y stays 1. x = x+y = 3. Result 3*10+1 = 31.

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

Related questions