Compound Boolean Expressions

AP Computer Science A· difficulty 4/5

<code>(s != null && s.length() > 0)</code> is safe because

  • A

    It's a runtime trick

  • B

    Short-circuit prevents calling length() on null

    check_circle
  • C

    Java auto-checks null

  • D

    Always throws NPE

Explanation

If s is null, the && stops at the first part; length() is not called.

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

Related questions