Review Questions

graphics/rq_icon.gif

6.3

Which statements are true?

Select the two correct answers.

  1. A subclass must define all the methods from the superclass.

  2. It is possible for a subclass to define a method with the same name and parameters as a method defined by the superclass.

  3. It is possible for a subclass to define a field with the same name as a field defined by the superclass.

  4. It is possible for two classes to be the superclass of each other.

6.4

Given the following classes and declarations, which statements are true?

// Classes
class Foo {
    private int i;
    public void f() { /* ... */ }
    public void g() { /* ... */ }
}

class Bar extends Foo {
    public int j;
    public void g() { /* ... */ }
}

// Declarations:
// ...
    Foo a = new Foo();
    Bar b = new Bar();
// ...

Select the three correct answers.

  1. The Bar class is a legal subclass of Foo.

  2. The statement b.f(); is legal.

  3. The statement a.j = 5; is legal.

  4. The statement a.g(); is legal.

  5. The statement b.i = 3; is legal.

6.5

Which statement is true?

Select the one correct answer.

  1. Private methods cannot be overridden in subclasses.

  2. A subclass can override any method in a superclass.

  3. An overriding method can declare that it throws more exceptions than the method it is overriding.

  4. The parameter list of an overriding method must be a subset of the parameter list of the method that it is overriding.

  5. The overriding method can have a different return type than the overridden method.

6.6

Given classes A, B, and C, where B extends A, and C extends B, and where all classes implement the instance method void doIt(). How can the doIt() method in A be called from an instance method in C?

Select the one correct answer.

  1. doIt();

  2. super.doIt();

  3. super.super.doIt();

  4. this.super.doIt();

  5. A.this.doIt();

  6. ((A) this).doIt();

  7. It is not possible.

6.7

What would be the result of attempting to compile and run the following code?

// Filename: MyClass.java
public class MyClass {
    public static void main(String[] args) {
        C c = new C();
        System.out.println(c.max(13, 29));
    }
}

class A {
    int max(int x, int y) { if (x>y) return x; else return y; }
}

class B extends A{
    int max(int x, int y) { return super.max(y, x) - 10; }
}

class C extends B {
    int max(int x, int y) { return super.max(x+10, y+10); }
}

Select the one correct answer.

  1. The code will fail to compile because the max() method in B passes the arguments in the call super.max(y, x) in the wrong order.

  2. The code will fail to compile because a call to a max() method is ambiguous.

  3. The code will compile without errors and will print 13 when run.

  4. The code will compile without errors and will print 23 when run.

  5. The code will compile without errors and will print 29 when run.

  6. The code will compile without errors and will print 39 when run.

6.8

Given the following code, which is the simplest print statement that can be inserted into the print() method?

// Filename: MyClass.java
public class MyClass extends MySuperclass {
    public static void main(String[] args) {
        MyClass object = new MyClass();
        object.print();
    }
    public void print() {
        // INSERT CODE HERE THAT WILL PRINT
        // THE "Hello, world!" STRING FROM THE Message
        // CLASS.
    }
}

class MySuperclass {
    Message msg = new Message();
}

class Message {
    // The message that should be printed:
    String text = "Hello, world!";
}

Select the one correct answer.

  1. System.out.println(text);

  2. System.out.println(Message.text);

  3. System.out.println(msg.text);

  4. System.out.println(object.msg.text);

  5. System.out.println(super.msg.text);

  6. System.out.println(object.super.msg.text);



     
    ASPTreeView.com
     
    Evaluation has ДСБГexpired.
    Info...