Tuesday, November 26, 2013

Question: Typecasting and Array

Given:

public static void main(String[] args) {
  Object obj = new int[] { 1, 2, 3 };
  int[] someArray = (int[])obj;

  for (int i : someArray) System.out.print(i + " ");
}


What is the result?
A. 1 2 3
B. Compilation fails because of an error in line Object obj .
C. Compilation fails because of an error in line int[] someArray.
D. Compilation fails because of an error in line for (int i.
E. A ClassCastException is thrown at runtime.




































Execution:
Correct declaration and casting
and correct fetching of data
 
Answer: A

No comments:

Post a Comment