Given the following program, which statements are true?
A. Compile time error at line 3.
B. The program will throw a j ava.lang.ClassCastException at the line labelled 2 when
run.
C. The program will throw a j ava.lang.ClassCastException at the line labelled 3 when run.
D. The program will compile and run if the (B[ ] ) cast in the line 2 and the whole line 3 is removed.
E. The cast at line 2 is needed.
Correct Options are : C E
but according to me if C is correct then why not B. As a1 and a both refer to the same array object.
Paul Clapham wrote:But they don't. Not at the line with the comment //2 at the end, anyway.
correct me if I am wrong
in starting both a and a1 were pointing to the same new A[10] but after //1 a and b will refer to new B[20] and a1 will refer to new A[10].
so no error or exception is at //2
Yes, that looks right to me. (And it makes the answers in your original post come out correct, too.)
By the way could you please tell us where that code came from? Read our FAQ entry QuoteYourSources (<-- click that link) for information about why we want to know that.
Paul Clapham wrote:By the way could you please tell us where that code came from? Read our FAQ entry QuoteYourSources (<-- click that link) for information about why we want to know that.