• 沒有找到結果。

public int bar( int x ) { return x; }

在文檔中 試題試題試題試題 1. (頁 51-110)

Add method to the Beta class to make it compile correctly.

class Alpha {

public void bar(int… x) {}

public void bar(int x) {}

}

public class Beta extends Alpha { 1.

2.

3.

}

Methods:

1. 2. 3.

B C E

Public class Beta extends Alpha { public void bar( int x ) {}

public int bar( String x ) { return 1; } public void bar( int x, int y ) {}

}

【參考章節】2-4-1 覆寫

【試題解析】

private void bar( int x ) {} 錯誤。覆寫時,權限不可小於原方法。

public void bar( int x ) {} 正確。覆寫的機制。

public int bar( String x ) { return 1; } 正確。多載機制,方法名稱一樣,但傳入參數必 須不同。

public Alpha bar( int x ) {} 錯誤。覆寫時的傳回值可以與原方法相等或是原方法的直 系子類別,也就是共變回傳(covariant return),不過本選項的原方法是void,因此子類別 只能用void來修飾bar()方法。

public void bar( int x, int y ) {} 正確。多載機制,為何不是覆寫呢?因為要寫成 public void bar(int…x){}才是覆寫。

public int bar( int x ) { return x; } 錯誤。覆寫時的傳回值可以與原方法相等或是原方 法的直系子類別。

Given:

1. public class Blip{

2. protected int blipvert(int x) { return 0; } 3. }

4. class Vert extends Blip { 5. //insert code here 6. }

Which five methods, inserted independently at line 5, will compile? (Choose five) A. public int blipvert(int x){ return 0; }

B. private int blipvert(int x ){ return 0; } C. private int blipvert(long x){ return 0; } D. protected long blipvert(int x){ return 0; } E. protected int blipvert(long x){ return 0; } F. protected long blipvert(long x){ return 0; } G. protected long blipvert(int x, int y){ return 0; }

【答案】ACEFG

【參考章節】2-4 覆寫與多載

【試題解析】

選項A:正確。正確的覆寫。

選項B:錯誤。存取權限不可以比原方法更侷限。

選項C:正確。多載blipvert(),因為傳入的參數不一樣。

選項D:錯誤。只改變回傳參數,而又不符合共變回傳(covariant return)。

選項E:正確。同選項C。

選項F:正確。多載,改變了回傳型別以及參數型別。

選項G:正確。同上。

Given classes defined in two different files:

1. package packageA;

2. public class Message {

3. String getText() { return “text”; } 4. }

And:

1. package packageB;

2. public class XMLMessage extends packageA.Message { 3. String getText() { return “<msg>text</msg>”; } 4. public static void main(String[] args){

5. System.out.println(new XMLMessage().getText());

6. } 7. }

What is the result of executing XMLMessage.main?

A. text

B. <msg>text</msg>

C. An exception is thrown at runtime.

D. Compilation fails because of an error in line 2 of XMLMessage.

E. Compilation fails because of an error in line 3 of XMLMessage.

【答案】B

【參考章節】2-4 覆寫/多載

【試題解析】

本題在IDE上編譯時,會出現以下的警告訊息:

” Warning: Chapter1\packageB\XMLMessage.java modified in the future.”

但還是可以正常編譯,執行結果為B。但是在覆寫觀念上,父類別被繼承後,在其它子 類別若方法的存取修飾子不是設為public或protected的,在覆寫父類別方法時,將會出現 錯誤!

Given the following Java code:

10. class Test1{

11. public Test1 foo(){ return this; } 12. }

13. class Test2 extends Test1{

14. public Test1 foo(){ return this; } 15. }

16. class Test3 extends Test2 { 17. // insert method here 18. }

Which two methods, inserted individually, correctly complete the Test3 class? {Choose two.}

A. public void foo(){ }

B. public int foo(){ return 3; } C. public Test2 foo(){ return this; } D. public Test1 foo(){ return this; }

【答案】CD

【參考章節】2-4-1 覆寫函式

【試題解析】

覆寫時的傳回值可以與原方法相等或是原方法的直系子類別,也是所謂的共變回傳 (covariant return)。因為Test2 extends Test1,所以回傳型別為Test2或Test1的覆寫皆是正確 的。

Given the following Java code:

10. class One{

11. void foo(){ } 12. }

13. class Two extends One{

14. // insert method here 15. }

Which three methods, inserted individually at line 14 will correctly class Two?(Choose three.)

A. int foo(){ /* more code here */ } B. void foo(){ /* more code here */ } C. public void foo(){ /* more code here */ } D. private void foo(){ /* more code here */ } E. protected void foo(){ /* more code here */ }

【答案】BCE

【參考章節】2-4-1 覆寫函式

【試題解析】

選項A:錯誤。若覆寫的原方法回傳型別為void,則覆寫方法的回傳型別也必須為 void。

選項B:正確。

選項C:正確。

選項D:錯誤。覆寫方法的權限不可小於原方法。

選項E:正確。

Given the following Java code:

Class SomeException:

1. public class SomeException{

2. }

Class A:

1. public class A{

2. public void doSomething(){}

3. }

Class B:

1. public class B extends A{

2. public void doSomething() throws SomeException{}

3. }

Which statement is true about the two classes?

A. Compilation of both classes will fail.

B. Compilation of both classes will succeed.

C. Compilation of class A will fail, Compilation of class B will succeed.

D. Compilation of class B will fail, Compilation of class A will succeed.

【答案】D

【參考章節】2-4-1 覆寫函式

【試題解析】

覆寫的限制與注意事項中,關於方法例外修飾throws:「當原方法有引發例外事件,

覆寫方法也可引發例外事件,但此例外要包含在原方法所引發的例外事件內(相等或不 寫也可以)。」

本題原方法doSomething()並沒有throws例外,因此覆寫方法並不需要利用throws來 修飾方法例外。

Given the following Java code:

1. public classA{

2. public void doit(){

3. }

4. public String doit(){

5. return “a”;

6. }

7. public double doit(int x){

8. return 1.0;

9. } 10. }

What is the result?

A. An exception is thrown at runtime.

B. Compilation fails because of an error in line 7.

C. Compilation fails because of an error in line 4.

D. Compilation succeeds and no runtime errors with class A occur.

【答案】C

【參考章節】2-4-2 多載函式

【試題解析】

多載函式可以改變其回傳型別,但是必須改變該方法的傳入參數。

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 AImp1 doit() { 3. // more code here 4. }

5.

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

9. }

Which statement is true about the class and interfaces in this “Code”?

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 AImpl will fail because of an error in line 2.

【答案】C

【參考章節】2-4-1 覆寫函式

【試題解析】

Class C的第六行,覆寫函式必須是原來函式的回傳型別或則是它的子型別。

Given:

10. abstract public class Employee {

11. protected abstract double getSalesAmount();

12. public double getCommision(){

13. return getSalesAmount() * 0.15;

14. } 15. }

16. class Sales extends Employee{

17. // inset method here 18. }

Which two methods, inserted independently at line 17, correctly complete the Sales class?

(Choose two.)

A. double getSalesAmount(){ return 1230.45; } B. public double getSalesAmount(){return 1230.45; } C. private double getSalesAmount(){return 1230.45; } D. protected double getSalesAmount(){return 1230.45; }

【答案】BD

【參考章節】2-4-1 覆寫函式

【試題解析】

本題繼承abstract方法並覆寫它,須滿足覆寫規則”存取權限不得小於原方法的存取 權限”,故本題答案是BD。

Given code in separate source files:

1. public class Foo{

2. public int a;

3. public Foo(){ a = 3; }

4. public void addFive(){ a += 5}

5. } And:

1. public class Bar extends Foo{

2. public int a;

3. public Bar() {a = 8;}

4. public void addFive() {}

5. } invoked with:

25. Foo foo = new Bar();

26. foo.addFive();

27. System.out.println(“Value: ” + foo.a);

What is the result?

A. Value: 3 B. Value: 8 C. Value: 13

D. Compilation fails.

E. The code runs with no output.

F. An exception is thrown at runtime.

【答案】A

【參考章節】2-4-1 覆寫函式

【試題解析】

當覆寫發生時,則執行方法時會依照實體來決定執行哪一個方法;但如果是存取實 體變數(instance variable)則會依照參考變數的型別。

故執行第26行時,是執行子類別Bar的addFive()方法。而第27行則是存取Foo類別的a。

Given the following Java code:

1. public class SimpleCalc{

2. public int value;

3. public void calculate(){ value += 7; } 4. }

And:

1. public class MultiCalc extends SimpleCalc{

2. public void calculate() { value –= 3 ; } 3. public void calculate( int multiplier ) { 4. calculate();

5. super.calculate();

6. value *= multiplier;

7. }

8. public static void main(String[] args){

9. MultiCalc calculator = new MultiCalc();

10. calculator.calculate(2);

11. System.out.println( “Value is: ” + calculator.value );

12. } 13. }

What is the result?

A. Value is:8 B. Compilation fails.

C. Value is:12 D. Value is:-12

E. The code runs with no output.

F. An exception is thrown at runtime.

【答案】A

【參考章節】2-4-2 多載函式

【試題解析】

Line 9: value = 0 (預設)

Line10:呼叫calculator.calculate(2) Line3: value = 0, multiplier = 2

Line4: 呼叫calculate()因此value = 0 – 3 = -3, multiplier = 2

Line5: 呼叫super.calculate() 因此value = -3 + 7 = 4 , multiplier = 2 Line6: 執行 value *= multiplier = 8

Line11: 印出 Value is: 8

Given the following Java code:

11. public static void main(String[] args){

12. Object obj = new int[] { 1, 2, 3 };

13. int[] someArray = (int []) obj;

14. for( int x : someArray ) System.out.println(x + “ ”);

15. }

What is the sesult?

A. 1 2 3

B. Compilation fails because of an error in line 12.

C. Compilation fails because of an error in line 13.

D. Compilation fails because of an error in line 14.

E. A ClassCaseException is thrown at runtime.

【答案】A

【參考章節】2-5 參考變數的轉型(Casting)

【試題解析】

第13行將obj轉型成 int[] ,因為第12行的對應中obj的實體就是int[]的物件,第14行 利用for-each語句將int[]物件的內容值印出。

Given the following Java code:

1. class TestA{

2. public void start() { System.out.println(“TestA”); } 3. }

4. public class TestB extends TestA {

5. public void start() { System.out.println(“TestB”) } 6. public static void main(String[] args){

7. ((TestA)new TestB()).start();

8. } 9. }

What is the result?

A. TestA B. TestB

C. Compilation fails.

D. An exception is thrown at runtime.

【答案】B

【參考章節】2-5 參考變數的轉型(Casting)

【試題解析】

第7行對Test B進行向上轉型(upcasting)為Test A,這代表限制了Test B的實體只能使 用繼承自Test A的方法。但是這並不影響所要執行的實體,所以第7行仍是執行實體Test B 的start(),執行結果為”TestB”。

Given the following Java code:

1. interface A { public void aMethod(); } 2. interface B { public void bMethod(); }

3. interface C extends A, B { public void cMethod(); } 4. class D implements B {

5. public void bMethod() {}

6. }

7. class E extends D implements C { 8. public void aMethod(){}

9. public void bMethod(){}

10. public void cMethod(){}

11. }

What is the result?

A. Compilation fails because of an error in line 3.

B. Compilation fails because of an error in line 7.

C. Compilation fails because of an error in line 9.

D. If you define D e = new E(), then e.bMethod() invokes the version of bMethod() defined in line 5.

E. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in line 5.

F. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in line 9.

【答案】F

【參考章節】2-5 參考變數的轉型(Casting)

【試題解析】

本題將會通過編譯,所以ABC是錯誤的。

選項D:因為執行時期依然是執行E的實體,所以應該是執行第9行的bMethod()。

選項E:理由同上。

選項F:正確。

Given the following Java code:

10. interface Foo {}

11. class Alpha implements Foo {}

12. class Beta extends Alpha {}

13. class Delta extends Beta {

14. public static void main(String[] args) { 15. Beta x = new Beta();

16. // insert code here 17. }

18. }

Which code, inserted at line 16 will cause a java.lang.ClassCastException?

A. Alpha a = x;

B. Foo f = (Delta)x C. Foo f = (Alpha)x

D. Beta b = (Beta)(Alpha)x;

【答案】B

【參考章節】2-5 參考變數的轉型(Casting)

【試題解析】

x的實體是new Beta(),因此可以合法的轉型成Beta、Alpha與Foo,但不能轉型成子 類別Delta。

Given:

11. class ClassA{}

12. class ClassB extends ClassA {}

13. class ClassC extends ClassA {}

And:

26. ClassA p0 = new ClassA();

27. ClassB p1 = new ClassB();

28. ClassC p2 = new ClassC();

29. ClassA p3 = new ClassB();

30. ClassA p4 = new ClassC();

Which three are valid? (Choose three.) A. p0 = p1;

B. p1 = p2;

C. p2 = p4;

D. p2 = (ClassC)p1;

E. p1 = (ClassB)p3;

F. p2 = (ClassC)p4;

【答案】AEF

【參考章節】2-5 參考變數的轉型(Casting)

【試題解析】

選項A. 正確。ClassB 與 ClassA有繼承關係,所以p0 可以指向 p1的參考。

選項B. 錯誤。ClassB 與 ClassC並無繼承關係,所以p1 = p2將出現錯誤。

選項C. 錯誤,雖然兩者有繼承關係,但子類別的參考若沒將父類別的參考進行向 下轉型的動作,則p2 = p4將出現編譯錯誤。

選項D. 錯誤。理由同B。

選項E. 正確。正確的將父類別向下轉型成子類別型態。

選項F. 正確。正確的將父類別向下轉型成子類別型態。

Given the following Java code:

11. class Animal { public String noise() { return “peep”; } } 12. class Dog extends Animal {

13. public String noise() { return “bark”; } 14. }

15. class Cat extends Animal {

16. public String noise() { return “meow”; } 17. }

30. Animal animal = new Dog();

31. Cat cat = (Cat) animal;

32. System.out.println( cat.noise() );

What is the result?

A. peep B. bark C. meow

D. Compilation fails.

E. An exception is thrown at runtime.

【答案】E

【參考章節】2-3 多型

【試題解析】

本題編譯會通過,但是在程式第31行:

Cat cat = (Cat) animal;

執行時期會發生runtime exception。因為animal的實體是Dog(),Dog怎麼可以轉型成 Cat?在編譯時期會有沒錯誤是因為Dog與Cat有共同的Animal父類別,所以我們通常會 利用instanceof來判斷兩者是否有繼承關係。

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 1. 2. { 3.

}

Code Fragments:

A. extends C. public void display(); E. Reloadable

B. implements D. Public void display(){/*Display*/} F. Edit

1. 2. 3.

【答案】

1. 2. 3.

A E C

interface Reloadable { public void reload( );

}

class Edit {

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

}

interface Displayable extends Reloadable { public void display( );

}

【參考章節】2-5 實作介面

【試題解析】

本題要完成Displayable的介面,因為介面不可以繼承類別,所以可以推導出本題要 繼承Reloadable介面,介面繼承介面的識別字為extends,而且介面內部不可以實作任何 程式碼。

Which two classes correctly implement both the java.lang.Runnable and the java.lang.Cloneable interface? (Choose two.)

A. public class Session implements Runnable,Cloneable { public void run();

public Object clone();

}

B. public class Session extends Runnable, Cloneable { public void run() {/*do something */}

public Object clone(){/* do something */}

}

C. public class Session implements Runnable, Cloneable { public void run() {/*do something */}

public Object clone(){/* do something */}

}

D. public abstract class Session implements Runnable, Cloneable { public void run() {/*do something */}

public Object clone(){/* do something */}

}

E. public class Session implements Runnable, implements Cloneable { public void run() {/*do something */}

public Object clone(){/* do something */}

}

【答案】CD

【參考章節】2-5 實作介面

【試題解析】

選項A: 錯誤。沒有實作所有抽象方法。

選項B: 錯誤。想要實作介面必須透過implements宣告。

選項C: 正確。

選項D: 正確。

選項E: 錯誤。extends與implements在同一方法只能出現一次。

Given the following Java code:

1. interface DoStuff2{

2. float getRange(int low, int high); } 3.

4. interface DoMore{

5. float getAvg(int a, int b, int c); } 6.

7. abstract class DoAbstract implements DoStuff2, DoMore {}

8.

9. class DoStuff implements DoStuff2{

10. public float getRange(int x, int y) { return 3.14f; } } 11.

12. interface DoAll extends DoMore{

13. float getAvg(int a, int b, int c, int d); } What is the result?

A. The file will compile without error.

B. Compilation fails. Only line 7 contains an error.

C. Compilation fails. Only line 12 contains an error.

D. Compilation fails. Only line 13 contains an error.

E. Compilation fails. Only line 7 and 12 contains an error.

F. Compilation fails. Only line 7 and 13 contains an error.

G. Compilation fails. Lines 7, 12, and 13 contain errors.

【答案】A

【參考章節】2-5 實作介面

【試題解析】

 介面不可有實作方法。

 類別必須利用implements字眼來實作介面。

 非抽象函式必須實作所有介面的函式。

 介面與介面的擴充必須透過extends字眼。

Given the following Java code:

1. public class GoTest{

2. public static void main(String[] args) { 3. Sente a = new Sente(); a.go();

4. Goban b = new Goban(); b.go();

5. Stone c = new Stone(); c.go();

6. } 7. } 8.

9. class Sente implements Go{

10. public void go(){ System.out.println(“go in Sente.”); } 11. }

12.

13. class Goban extends Sente{

14. public void go() { System.out.println(“go in Goban”); } 15. }

16.

17. class Stone extends Goban implements Go { } 18.

19. interface Go { public void go(); } What is the result?

A. go in Goban go in Sente go in Goban B. go in Sente

go in Goban go in Goban C. go in Goban

go in Serte go in Serte

D. Compilation fails because of an error in line 17.

【答案】B

【參考章節】2-5 實作介面

【試題解析】

本題可以通過編譯,且當執行第3行Snete a = new Snete(); a.go();將印出”go in Sente”,只有選項B符合。接下來陸續印出“go in Goban”、 “go in Goban”,至於第5行因 為沒覆寫go()的函式,所以會執行父類別Goban版本的go()。

Given the following Java code:

1. interface Data { public void load(); }

2. abstract class Info { public abstract void load(); }

Which class correctly uses the Data interface and Info class?

A. public class Employee extends Info implements Data { public void load() { /*do something*/ }

}

B. public class Employee implements Info extends Data { public void load() { /*do something*/ }

}

C. public class Employee extends Info implements Data { public void load() { /*do something*/ }

public void Info.load() { /*do something*/ } {

D. public class Employee implements Info extends Data { public void Data.load() { /*do something*/ }

public void load() { /*do something*/ } }

E. public class Employee implements Info extends Data { public void load() { /*do something*/ }

public void Info.load() { /*do something*/ } }

F. public class Employee extends Info implements Data { public void Data.load() { /*do something*/ }

public void load() { /*do something*/ } }

【答案】A

【參考章節】2-6 實作介面

【試題解析】

有關extends與implements的定義順序,應先定義extends再定義implements,且extends 與implements這兩個關鍵字最多只能各出現一次 因此選項BDE是錯的。另外,方法名稱 Info.load與Data.load是錯誤的命名(名稱不可以含有”.”),選項C與F也是錯的,因此本題 答案為選項A。

Given the following Java code:

10. public class ClassA { 11. public void mehtodA() {

12. ClassB classB = new ClassB();

13. classB.getValue();

14. } 15. } And:

20. class ClassB {

21. public ClassC classC;

22.

23. public String getValue() { 24. return classC.getValue();

25. } 26. } And:

30. class ClassC {

31. public String value;

32.

33. public String getValue() { 34. value = “ClassB”;

35. return value;

36. } 37. } And:

ClassA a = new ClassA();

a.methodA();

What is the result?

A. Compilation fails.

B. ClassC is displayed.

C. The code runs with no output.

D. An exception is thrown at runtime.

【答案】D

【參考章節】2-7 合法的回傳型別

【試題解析】

第24行的return classC.getValue() 由於沒有classC並沒有指向任何實體,所以在存取 getValue的時候會丟出”java.lang.NullPointerException”。

在文檔中 試題試題試題試題 1. (頁 51-110)

相關文件