第五十一題:

Given:

1. public interface A{
2.     public void doSomething(String thing);
3. }

1. public class AImpl implements A{
2.     public void doSomething(String msg){}
3. }

1. public class B{
2.     public A doit(){
3.         //more code here
4.     }

5.
6.     public String execute(){
7.         //more code here
8.     }

9. }

1. public class C extends B{
2.     public AImpl doit(){
3.         //more code here
4.     }

5.
6.     public Object execute(){
7.         //more code here
8.     }

9. }

Which statement is true about the classes and interfaces?

A. Compilation will succeed for all classes and interfaces.
B. Compilation of class C will fail because of an error in line 2.
C. Compilation of class C will fail because of an error in line 6.
D. Compilation of class AImpl will fail because of an error in line 2.

答案:

執行結果:無

題目範圍:物件觀念、Interface

解析:

選項C的錯誤原因應該是因為共變回傳(covariant return)
類別C繼承類別B,所以覆寫execute()時類別C的回傳不可以是類別B回傳的父類,必須為子類別
(類別B的execute()回傳String,類別C的execute()則回傳Object,對調則順利編譯)
--感謝kevin wang 熱心補充

Interface是可以被拿來做為回傳值的,你可以將一個物件適當的轉成Interface物件,但不能用Interface類別來宣告Interface物件

 

第五十二題 (拖拉題)

Place the code fragments in position to complete the Displayable interface.

interface Reloadable {
    public void reload();
}

class Edit {
    public void edit() {/* Edit Here */
    }

}
interface Displayable extends Reloadable {
    public void display();
}

Code Fragments

extends

public void display();

Reloadable

implements

public void display(){/* Diaplay */}

Edit

 

答案:題目反白

執行結果:無

題目範圍:物件觀念、Interface

解析:

這是介面之間的繼承關係,介面只能夠繼承介面,不可以繼承一般類別或是抽象類別
介面中的方法也都必須是空白的

 

第五十三題:

Insert six modifiers into the code such that it meets all of these requirements:

1. It must be possible to create instances of Alpha and Beta from outside the packages in which they are defined.
2. When an object of type Alpha (or any potential subclass of Alpha) has been created, the instance variable alpha may never be changed.
3. The value of the instance variable alpha must always be "A" for objects of type Alpha.

Code

package alpha;

public class Alpha{

     private String alpha;

     public Alpha(){this("A");}

    protected Alpha(String a){alpha = a;}

}

 

package beta;

public class Beta extends alpha.Alpha{

    public Beta(String a){super(a);}

}

Modifiers

private

protected

public

 

答案:題目反白

執行結果:無

題目範圍:物件觀念

解析:

類別與方法的標記作用範圍

 

當前類別

package類別

子代類別

所有類別

Public

Protected

(default)

private

這個表格十分重要,必須牢記但是不要死背,多運用便可了解

這題比較特別的地方應該是標記為protected的建構子
這樣一來在別的地方實體化alpha時,便不可使用
protected的建構子
只有alpha的子代,beta可以用這個建構子

一般的建構子需要標記為public,否則無法被外部使用

 

 

第五十四題:

Given:

package test;

class Target{
    public String name = "hello";
}

What can directly access and change the value of the variable name?

A. any class
B. only the Target class
C. any class in the test package
D. any class that extends Target

答案:

執行結果:無

題目範圍:物件觀念

解析:

雖然變數name被標記為public,但是所屬的類別是(default)的,因此要使用該變數還是得在同一個package底下才行

 

第五十五題:

Given:

11. abstract class Vehicle{public int speed(){return 0;}}
12. class Car extends Vehicle{public int speed(){return 60;}}
13. class RaceCar extends Car{public int speed(){return 150;}}
...
21. RaceCar racer = new RaceCar();
22. Car car = new RaceCar();
23. Vehicle vehicle = new RaceCar();
24. System.out.println(racer.speed() + ", " + car.speed()
25. + ", " + vehicle.speed());

What is the result?

A. 0, 0, 0
B. 150, 60, 0
C. Compilation fails.
D. 150, 150, 150
E. An exception is thrown at runtime.

答案:

執行結果:無

題目範圍:物件觀念、abstract class

解析:

不論型態如何轉換,物件永遠會保持他一開始的樣子,呼叫方法時會呼叫原本的方法

abstract class不可以用來宣告物件,不過可以被拿來轉換型態,類似Interface

arrow
arrow

    yaya741228 發表在 痞客邦 留言(4) 人氣()