Review Questions

6.17 Given the following program, which statement is true?
// Filename: MyClass.java
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.

  1. The program will fail to compile because of the assignment at (1).

  2. The program will throw a java.lang.ClassCastException in the assignment at (2) when run.

  3. The program will throw a java.lang.ClassCastException in the assignment at (3) when run.

  4. The program will compile and ...

Get Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.