Examveda
Examveda

Predict the output:
public class Test{     
      public static void main(String args[]){
            try{
                  String arr[] = new String[10];
                  arr = null;
                  arr[0] = "one";
                  System.out.print(arr[0]);
            }catch(Exception ex){
                  System.out.print("exception");
            }catch(NullPointerException nex){
                  System.out.print("null pointer exception");
            }   
      }
}

A. "one" is printed.

B. "exception" is printed.

C. "null pointer exception" is printed.

D. Compilation fails saying NullPointerException has already been caught.

E. None of these

Answer: Option D


This Question Belongs to Java Program >> Exceptions

Join The Discussion

Comments ( 1 )

  1. Vikyath Srinivasulu
    Vikyath Srinivasulu :
    1 year ago

    More specified exceptions need to be caught first. Here the general exception is caught first so it will give a compilation error.

Related Questions on Exceptions