Compound Boolean Expressions

AP Computer Science A· difficulty 4/5

public static int eval() {
    int a = 0;
    int b = 0;
    boolean t = (a++ > 0) && (b++ > 0);
    return a * 10 + b;
}
// Call: System.out.println(eval());

What is printed?

  • A

    11

  • B

    0

  • C

    1

  • D

    10

    check_circle

Explanation

a++ returns 0 (not >0); a becomes 1. && short-circuits so b++ never runs. b=0. Result 1*10+0 = 10.

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

Related questions