| 6.3 | Which statements are true? Select the two correct answers. -
A subclass must define all the methods from the superclass. -
It is possible for a subclass to define a method with the same name and parameters as a method defined by the superclass. -
It is possible for a subclass to define a field with the same name as a field defined by the superclass. -
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. -
The Bar class is a legal subclass of Foo . -
The statement b.f(); is legal. -
The statement a.j = 5; is legal. -
The statement a.g(); is legal. -
The statement b.i = 3; is legal. |
| 6.5 | Which statement is true? Select the one correct answer. -
Private methods cannot be overridden in subclasses. -
A subclass can override any method in a superclass. -
An overriding method can declare that it throws more exceptions than the method it is overriding. -
The parameter list of an overriding method must be a subset of the parameter list of the method that it is overriding. -
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. -
doIt(); -
super.doIt(); -
super.super.doIt(); -
this.super.doIt(); -
A.this.doIt(); -
((A) this).doIt(); -
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. -
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. -
The code will fail to compile because a call to a max() method is ambiguous. -
The code will compile without errors and will print 13 when run. -
The code will compile without errors and will print 23 when run. -
The code will compile without errors and will print 29 when run. -
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. -
System.out.println(text); -
System.out.println(Message.text); -
System.out.println(msg.text); -
System.out.println(object.msg.text); -
System.out.println(super.msg.text); -
System.out.println(object.super.msg.text); |