Java OCA OCP Practice Question 2621

Question

Given:

1. public class Main {  
2.   public static void main(String[] args) {  
3.     int [][] ia2;  
4.     int [] ia1 = {1,2,3};  
5.     Object o = ia1;  
6.     ia2 = new int[3][3];  
7.     ia2[0] = (int[])o;  
8.     ia2[0][0] = (int[])o;  
9. } } 

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

  • A. Compilation fails due to an error on line 4.
  • B. Compilation fails due to an error on line 5.
  • C. Compilation fails due to an error on line 6.
  • D. Compilation fails due to an error on line 7.
  • E. Compilation fails due to an error on line 8.
  • F. Compilation succeeds and the code runs without exception.
  • G. Compilation succeeds and an exception is thrown at runtime.


E is correct.

Note

Remember that arrays are objects, and that each array dimension is a separate type.

So, for instance, ia2 is of type "two dimensional int array", which is a different type than ia1.

Line 8 attempts to assign a one-dimensional array into an int.

A, B, C, and D are incorrect because lines 4-7 perform legal array manipulations.

F and G are incorrect based on the above.




PreviousNext

Related