What will be the output of the following Java program? [code] class exception_handling { public static void main(String args[]

Nguyễn Trà

New member
What will be the output of the following Java program?
Code:
    class exception_handling 
    {
        public static void main(String args[]) 
        {
            try 
            {
                int a = args.length;
                int b = 10 / a;
                System.out.print(a);
                try 
                {
                     if (a == 1)
                         a = a / a - a;
                     if (a == 2) 
                     {
                         int c = {1};
                         c[8] = 9;
                     }
                }
                catch (ArrayIndexOutOfBoundException e) 
                {
                    System.out.println("TypeA");
                }
                catch (ArithmeticException e) 
                {
                    System.out.println("TypeB");
                }
        }
    }
Note: Execution command line: $ java exception_handling one two
A. TypeA
B. TypeB
C. 0TypeA
D. 0TypeB
 
Answer: d
Explanation: Execution command line is "$ java exception_ handling one two" hence there are two input making args.length = 2, hence "c[8] = 9" in second try block is executing which throws ArrayIndexOutOfBoundException which is caught by catch of nested try block. Hence 0TypeB is printed.
Output:
Code:
$ javac exception_handling.java
$ java exception_handling
0TypeB
 

Tìm kiếm

Back
Top