What is the result?

Given:

1. public static void main(String[] args) {
2. try {
3. args = null;
4. args[0] = “test”;
5. System.out.println(args[0]);
6. } catch (Exception ex) {
7. System.out.println(“Exception”);
8. } catch (NullPointerException npe) {
9. System.out.println(“NullPointerException”);
10. }
11. }

What is the result?

Given:

1. public static void main(String[] args) {
2. try {
3. args = null;
4. args[0] = “test”;
5. System.out.println(args[0]);
6. } catch (Exception ex) {
7. System.out.println(“Exception”);
8. } catch (NullPointerException npe) {
9. System.out.println(“NullPointerException”);
10. }
11. }

What is the result?

A.
test

B.
Exception

C.
Compilation fails.

D.
NullPointerException

Explanation:
Main.java:8: exception java.lang.NullPointerException has already been caught
} catch (NullPointerException npe) {
^
1 error



Leave a Reply 1

Your email address will not be published. Required fields are marked *