Compound Boolean Expressions

AP Computer Science A· difficulty 4/5

String s = null;
if (s != null && s.length() > 0) {
    System.out.println("non-empty");
} else {
    System.out.println("empty or null");
}

What is printed?

  • A

    empty or null

    check_circle
  • B

    non-empty

  • C

    NullPointerException

  • D

    Nothing

Explanation

Short-circuit evaluation: s != null is false, so s.length() is never evaluated and no NPE occurs; the else branch runs.

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

Related questions