Hi,
In the programmer's guide to
java certification book i found this question:
Given the following program, which statement is true?
public class MyClass {
public static void main(
String[] args) {
A[] arrA;
B[] arrB;
arrA = new A[10];
arrB = new B[20];
arrA = arrB; // (1)
arrB = (B[]) arrA; // (2)
arrA = new A[10];
arrB = (B[]) arrA; // (3)
}
}
class A {}
class B extends A {}
Select the one correct answer.
a.The program will fail to compile because of the assignment at (1).
b.The program will throw a java.lang.ClassCastException in the assignment at (2) when run.
c.The program will throw a java.lang.ClassCastException in the assignment at (3) when run.
d.The program will compile and run without errors, even if the (B[]) cast in the statements at (2) and (3) is removed.
e.The program will compile and run without errors, but will not do so if the (B[]) cast in statements at (2) and (3) is removed
The answer was c.
why? please help.
Thank you.