if Statements and Control Flow

AP Computer Science A· difficulty 3/5

int x = 5;
int y = 0;
if (x > 0)
  y++;
  y++;
System.out.println(y);

What is printed?

  • A

    2

    check_circle
  • B

    1

  • C

    0

  • D

    compile error

Explanation

Without braces, only the FIRST y++ is conditional on the if. The second y++ always executes. With x > 0 true, both run. y becomes 2. (Indentation is misleading.)

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

Related questions