Review Questions

graphics/rq_icon.gif

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 run without errors, even if the (B[]) cast in the statements at (2) and (3) is removed.

  5. 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.

  1. Line labeled (1).

  2. Line labeled (2).

  3. Line labeled (3).

  4. Line labeled (4).

  5. Line labeled (5).

  6. 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.

  1. obj2 = obj1;

  2. obj3 = obj1;

  3. obj3 = obj2;

  4. I1 a = obj2;

  5. I1 b = obj3;

  6. 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.

  1. Illegal at compile time.

  2. Legal at compile time, but might be illegal at runtime.

  3. Definitely legal at runtime, but the (Sub) cast is not strictly needed.

  4. 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.

  1. c = d;

  2. d = c;

  3. A a = d;

  4. d = (D) c;

  5. 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.

  1. A will be printed.

  2. B will be printed.

  3. C will be printed.

  4. 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.

  1. (o instanceof B) && (!(o instanceof A))

  2. (o instanceof B) && (!(o instanceof C))

  3. !((o instanceof A) || (o instanceof B))

  4. (o instanceof B)

  5. (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.

  1. True.

  2. False.



     
    ASPTreeView.com
     
    Evaluation has ЧµМСЩјЙЪКЙ¶expired.
    Info...