Java OCA OCP Practice Question 200

Question

Given:

1. class Main {
2.   public static void main(String[] args) {
3.     int[][] a = {{1,2}, {3,4}};
4.     int[] b = (int[]) a[1];
5.     Object o1 = a;
6.     int[][] a2 = (int[][]) o1;
7.     int[] b2 = (int[]) o1;
8.     System.out.println(b[1]);
9. } }

What is the result? (Choose all that apply.)

  • A. 2
  • B. 4
  • C. An exception is thrown at runtime
  • D. Compilation fails due to an error on line 4
  • E. Compilation fails due to an error on line 5
  • F. Compilation fails due to an error on line 6
  • G. Compilation fails due to an error on line 7


P:C is correct.

Note

A ClassCastException is thrown at line 7 because o1 refers to an int[][], not an int[].

If line 7 were removed, the output would be 4.




PreviousNext

Related