Given: 11. public static void main(String[] args) { 12. try { 13. args=null; 14. args[0] = “test”; 15. System.out.println(args[0]); 16. } catch (Exception ex) { 17. System.out.println(”Exception”); 18. } catch (NullPointerException npe) { 19. System.out.println(”NullPointerException”); 20. } 21. } What is the result?

  1. test

  2. Exception

  3. Compilation fails.

  4. NullPointerException


Correct Option: C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) test - This option is incorrect because the code on line 13 assigns a null value to the args array, and then on line 14, it tries to access args[0] and assign it the value "test". Since args is null, a NullPointerException is thrown before reaching the System.out.println statement.

Option B) Exception - This option is incorrect because the catch block on line 16 catches any exception of type Exception. However, the exception that is thrown in this case is a NullPointerException, so the catch block on line 16 is not executed.

Option C) Compilation fails - This option is correct because on line 14, the code tries to assign a value to args[0] without first initializing the args array. This will result in a compilation error because the args array is null and cannot be accessed.

Option D) NullPointerException - This option is incorrect because the catch block on line 19 catches a NullPointerException, but since the code fails to compile, this block is not executed.

The correct answer is C) Compilation fails. This option is correct because attempting to assign a value to an uninitialized array (args[0]) will result in a compilation error.

Find more quizzes: