if Statements and Control Flow

AP Computer Science A· difficulty 3/5

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

What is printed?

  • A

    2

  • B

    -1

  • C

    1

    check_circle
  • D

    0

Explanation

The if controls only the first y++. With x = -1, that one is skipped. The second y++ is NOT inside the if (despite the indentation), so it always runs. y = 1.

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

Related questions