Multiple choice technology programming languages

public static void test(String str) { if (str == null | str.length() == 0) { System.out.println("String is empty"); } else { System.out.println("String is not empty"); } } And the invocation: test(null); What is the result?

  1. An exception is thrown at runtime.

  2. "String is empty" is printed to output

  3. Compilation fails

  4. "String is not empty" is printed to output.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The code uses the bitwise OR operator | instead of the logical OR operator ||. With |, both sides are always evaluated. When str is null, str.length() is still called, causing a NullPointerException at runtime. The fix is to use || which short-circuits: if str == null is true, str.length() is never evaluated.