Question discussion

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

1755Medium 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) {
        Object[] arr = new String[2];
        try {
            arr[0] = "x";
            arr[1] = 10;
            System.out.print("OK");
        } catch (ArrayStoreException e) {
            System.out.print("ASE");
        } catch (Exception e) {
            System.out.print("EX");
        }
    }
}
  • AASE
  • BOK
  • CEX
  • DCompilation error
Correct answer: A

Explanation

Arrays are covariant, so the code compiles, but the real array is a String[] and storing an Integer into it throws ArrayStoreException at run time.

Discussion 0

Nobody has replied yet. Start the discussion!