Multiple choice technology programming languages

Given: 11. public static void main(String[] args) { 12. String str = “null’; 13. if (str == null) { 14. System.out.println(”null”); 15. } else (str.length() == 0) { 16. System.out.println(”zero”); 17. } else { 18. System.out.println(”some”); 19. } 20. } ‘What is the result?

  1. null

  2. zero

  3. some

  4. Compilation fails.

  5. An exception is thrown at runtime.

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

Line 15 has a syntax error: 'else (str.length() == 0)' is missing the keyword 'if'. In Java, 'else' must be followed immediately by a statement or block, or by 'if' for an else-if clause. The parentheses alone make no sense syntactically. This causes compilation failure regardless of the string value being tested.