Given: 11. public static void main(String[] args) { 12. Object obj =new int[] { 1,2,3 }; 13. int[] someArray = (int[])obj; 14. for (int i: someArray) System.out.print(i +“ “) 15. } ‘What is the result?

  1. 1 2 3

  2. Compilation fails because of an error in line 12.

  3. Compilation fails because of an error in line 13

  4. Compilation fails because of an error in line 14


Correct Option: A
Explanation:

To solve this question, the user needs to know about arrays and casting in Java. The code initializes an Object obj to an array of integers with values 1, 2, and 3. It then casts the object to an array of integers and assigns it to the someArray variable. Finally, it uses a for-each loop to print out each element of the someArray array.

Now, let's go through each option and explain why it is right or wrong:

A. 1 2 3: This option is correct. The code initializes an array of integers with values 1, 2, and 3, casts it to an array of integers, and then prints out each element of the array using a for-each loop. This results in the output "1 2 3".

B. Compilation fails because of an error in line 12: This option is incorrect. Line 12 initializes an object to an array of integers, which is allowed in Java. There is no compilation error in this line.

C. Compilation fails because of an error in line 13: This option is incorrect. Line 13 casts the obj object to an array of integers, which is also allowed in Java. There is no compilation error in this line.

D. Compilation fails because of an error in line 14: This option is incorrect. Line 14 uses a for-each loop to print out each element of the someArray array, which is also allowed in Java. There is no compilation error in this line.

Therefore, the answer is:

The Answer is: A. 1 2 3

Find more quizzes: