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. -
The program will fail to compile because of the assignment at (1). -
The program will throw a java.lang.ClassCastException in the assignment at (2) when run. -
The program will throw a java.lang.ClassCastException in the assignment at (3) when run. -
The program will compile and run without errors, even if the (B[]) cast in the statements at (2) and (3) is removed. -
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. |
6.18 | Which is the first line that will cause compilation to fail in the following program? // Filename: MyClass.java class MyClass { public static void main(String[] args) { MyClass a; MySubclass b; a = new MyClass(); // (1) b = new MySubclass(); // (2) a = b; // (3) b = a; // (4) a = new MySubclass(); // (5) b = new MyClass(); // (6) } } class MySubclass extends MyClass {} Select the one correct answer. -
Line labeled (1). -
Line labeled (2). -
Line labeled (3). -
Line labeled (4). -
Line labeled (5). -
Line labeled (6). |
6.19 | Given the following definitions and reference declarations, which one of the following assignments is legal? // Definitions: interface I1 {} interface I2 {} class C1 implements I1 {} class C2 implements I2 {} class C3 extends C1 implements I2 {} // Reference declarations: // ... C1 obj1; C2 obj2; C3 obj3; // ... Select the one correct answer. -
obj2 = obj1; -
obj3 = obj1; -
obj3 = obj2; -
I1 a = obj2; -
I1 b = obj3; -
I2 c = obj1; |
6.20 | Given the following class definitions and the reference declarations, what can be said about the statement y = (Sub) x ? // Class definitions: class Super {} class Sub extends Super {} // Reference declarations // ... Super x; Sub y; // ... Select the one correct answer. -
Illegal at compile time. -
Legal at compile time, but might be illegal at runtime. -
Definitely legal at runtime, but the (Sub) cast is not strictly needed. -
Definitely legal at runtime, and the (Sub) cast is needed. |
6.21 | Given the following class definitions and declaration statements, which one of these assignments is legal at compile time? // Definitions: interface A {} class B {} class C extends B implements A {} class D implements A {} // Declaration statements: // [...] B b = new B(); C c = new C(); D d = new D(); // [...] Select the one correct answer. -
c = d; -
d = c; -
A a = d; -
d = (D) c; -
c = b; |
6.22 | Which letters will be printed when the following program is run? // Filename: MyClass.java public class MyClass { public static void main(String[] args) { B b = new C(); A a = b; if (a instanceof A) System.out.println("A"); if (a instanceof B) System.out.println("B"); if (a instanceof C) System.out.println("C"); if (a instanceof D) System.out.println("D"); } } class A {} class B extends A {} class C extends B {} class D extends C {} Select the three correct answers. -
A will be printed. -
B will be printed. -
C will be printed. -
D will be printed. |
6.23 | Given three classes A , B , and C , where B is a subclass of A and C is a subclass of B , which one of these boolean expressions is true when an object denoted by reference o has actually been instantiated from class B as opposed to from A or C ? Select the one correct answer. -
(o instanceof B) && (!(o instanceof A)) -
(o instanceof B) && (!(o instanceof C)) -
!((o instanceof A) (o instanceof B)) -
(o instanceof B) -
(o instanceof B) && !((o instanceof A) (o instanceof C)) |
6.24 | When the following program is run, it will print all the letters I , J , C, and D . Is this statement true or false? public class MyClass { public static void main(String[] args) { I x = new D(); if (x instanceof I) System.out.println("I"); if (x instanceof J) System.out.println("J"); if (x instanceof C) System.out.println("C"); if (x instanceof D) System.out.println("D"); } } interface I{} interface J{} class C implements I {} class D extends C implements J {} Select the one correct answer. -
True. -
False. |