Compound Boolean Expressions

AP Computer Science A· difficulty 3/5

if (s != null & s.length() > 0) {
    System.out.println(s);
}

What is wrong?

  • A

    Nothing is wrong

  • B

    length() should be length

  • C

    Single & does not short-circuit; replace with && to avoid NullPointerException

    check_circle
  • D

    & should be |

Explanation

The bitwise & evaluates both operands. When s is null, s.length() throws NullPointerException; && short-circuits and avoids the call.

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

Related questions

AP Computer Science A · Compound Boolean Expressions Practice Question | Acemy