if Statements and Control Flow

AP Computer Science A· difficulty 2/5

<code>if (x > 0); System.out.println("hello");</code> always prints "hello" because

  • A

    if-statement is ignored

  • B

    Trailing semicolon makes the if-body empty; the print runs unconditionally

    check_circle
  • C

    x > 0 always true

  • D

    Compile error

Explanation

The semicolon ends the if; print is no longer part of the if.

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

Related questions