Question discussion

What is the output of the following Java program?public class Main { public static void main(String[] args) { try { int[] a = new int[2]; a…

1542Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Java program?

public class Main {
    public static void main(String[] args) {
        try {
            int[] a = new int[2];
            a[2] = 5;
            System.out.print("A");
        } catch (ArithmeticException e) {
            System.out.print("X");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.print("B");
        } finally {
            System.out.print("C");
        }
        System.out.print("D");
    }
}
  • AACD
  • BXCD
  • CBD
  • DBCD
Correct answer: D

Explanation

a[2] is out of bounds, so "A" is skipped and the matching catch prints B; finally prints C and execution then continues to D.

Discussion 0

Nobody has replied yet. Start the discussion!