OCA Java SE 8 Mock Exam - OCA Mock Question 38








Question

Examine the following code and select the correct option(s):

public class Main { 
    public static void main(String args[]) { 
        Main var = new Main(); 
        var.print(args); 
    } 
    void print(String[] arr) { 
        try { 
            System.out.println(arr[0] + ":" + arr[1]); 
        } 
        catch (NullPointerException e) { 
            System.out.println("NullPointerException"); 
        } 
        catch (IndexOutOfBoundsException e) { 
            System.out.println("IndexOutOfBoundsException"); 
        } 
        catch (ArrayIndexOutOfBoundsException e) { 
            System.out.println("ArrayIndexOutOfBoundsException"); 
        } 
    } 
} 

a  If  the  class  Main  is  executed  using  the  following  command,  it 
   prints NullPointerException: 

java Main 

b  If  the  class  Main  is  executed  using  the  following  command,  it 
   prints IndexOutOfBoundsException: 

java Main 

c  If  the  class  Main  is  executed  using  the  following  command,  it 
   prints ArrayIndexOutOfBoundsException: 

java Main one 

    d  The code will fail to compile. 




Answer



D

Note

If an exception's base class is used in a catch block, it can catch all the exceptions of its derived class.

If you try to catch an exception from its derived class afterward, the code won't compile.

ArrayIndexOutOfBoundsException is a derived class of IndexOutOfBoundsException.