• 沒有找到結果。

試題試題試題試題 1.

N/A
N/A
Protected

Academic year: 2022

Share "試題試題試題試題 1."

Copied!
110
0
0

加載中.... (立即查看全文)

全文

(1)

Given:

35. String #name = "Jane Doe";

36. int $age = 24;

37. Double _height = 123.5;

38. double ~temp = 37.5;

Which two statements are true? (Choose two) A. Line 35 will not compile.

B. Line 36 will not compile.

C. Line 37 will not compile.

D. Line 38 will not compile.

【答案】AD

【參考章節】1-2-1 合法識別字

【試題解析】

識別字開頭除了英文字母、_、$之外,都是非法識別字。

試題 試題試題 試題 2.

A Java Bean component has the following field:

11. private Boolean enabled;

Which two pairs of method declarations follow the JavaBean standard for accessing this fields?

(Choose two)

A. public void setEnabled(Boolean enabled) public Boolean getEnabled()

B. public void setEnabled(Boolean enabled) public void isEnabled()

C. public void setEnabled(Boolean enabled) public Boolean isEnabled()

D. public Boolean setEnabled(Boolean enabled) public void getEnabled()

【答案】AC

【參考章節】1-2-3 JavaBean 標準

【試題解析】

選項A與選項C符合JavaBean標準。

選項B的pubic void isEnabled() → public Boolean,因為isEnabled()命名上,依照命 名標準應該是回傳一個Boolean的值。

選項D錯誤,因為兩個的回傳型別顛倒了。

(2)

Place the code elements in order so that the resulting Java source file will compile correctly, resulting in a class called “com.sun.cert.AddressBook”。

Source File

ArrayList entries;

}

1.

2.

3.

Code Element

A. package com.sun.cert;

B. package com.sun.cert.*;

C. import java.util.*;

D. import java.*;

E. public class AddressBook{

F. public static class AddressBook{

1 2 3

【答案】

1 2 3

A C E

Source File

ArrayList entries;

}

package com.sun.cert.;

import java.util.*;

public class AddressBook{

Code Element

package com.sun.cert;

package com.sun.cert.*;

import java.util.*;

import java.*;

public class AddressBook{

public static class AddressBook{

【參考章節】1-3-1 原始檔的宣告規則

【試題解析】

注意宣告上的順序:package → import → class

(3)

Given the following Java code:

11. interface DeclareStuff{

12. public static final int EASY = 3;

13. void doStuff(int t); }

14. class TestDeclare implements DeclareStuff{

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

16. int x = 5;

17. new TestDeclare().doStuff(++x);

18. }

19. void doStuff(int s){

20. s += EASY + ++s;

21. System.out.println("s " + s);

22. } 23. }

What is the result?

A. s 14 B. s 16 C. s 10

D. Compilation fails.

E. An exception is thrown at runtime.

【答案】D

【參考章節】1-4-1 宣告介面

【試題解析】

所有Interface內宣告的函式都隱含public abstract的。根據覆寫機制,其存取權限不 得小於原函式,本程式在第19行的存取權限為”Default”,所以編譯無法通過。若將19行 的存取權限為public,本題輸出答案為”s 16”。

(4)

Given the following Java code:

11. public interface Status{

12. /* insert code here */ int MY_VALUE = 10;

13. }

Which three are valid on line 12? (Choose three) A. final

B. static C. native D. public E. private F. abstract G. protected

【答案】ABD

【參考章節】1-4-2 宣告介面常數

【試題解析】

介面常數隱含public static final的,也就是說在interface內所宣告的常數以下都是正 確的且是一樣的:

public static final int x;

static final int x;

final int x;

int x;

(5)

Given the following Java code:

1. public interface A{

2. String DEFAULT_GREETING = “Hello World”

3. public void method1();

4. }

A programmer wants to create an interface called B that has A as its parent. Which interface declaration is correct?

A. public interface B extends A{}

B. public interface B implements A{}

C. public interface B instanceOF A{}

D. public interface B inheritsFrom A{}

【答案】A

【參考章節】1-4-1 宣告介面

【試題解析】

介面可以透過繼承(extends)來繼承多個介面。BCD在這題語法上都是錯誤的。

(6)

Given the following Java code:

1. public class Car{

2. private int wheelCount;

3. private String vin;

4. public Car(String vin){

5. this.vin = vin;

6. this.wheelCount = 4;

7. }

8. public String drive(){

9. return "zoom-zoom";

10. }

11. public String getInfo(){

12. return "VIN: " + vin + " wheels: " + wheelCount;

13. } 14. } And:

1. class MeGo extends Car{

2. public MeGo(String vin){

3. this.wheelCount = 3;

4. } 5. }

What two must the programmer do to correct the compilation errors?

A. insert a call to this() in the Car constructor.

B. insert a call to this() in the MeGo constructor.

C. insert a call to super() in the MeGo constructor.

D. insert a call to super(vin) in the MeGo constructor.

E. change the wheelCount variable in Car to protected.

F. change line 3 in the MeGo class to super wheelCount = 3.

【答案】DE

【參考章節】2-8-1 判斷是否會產生預設建構子 1-5-1 存取修飾子

【試題解析】

由於Car類別的第四行宣告了帶有參數的建構子Car(String vin),在編譯時候,系統 不會產生預設的建構子Car(),造成MeGo無法呼叫父類別的建構子,所以子類別要自行 呼叫帶有參數的父類別建構子super(vin)。

另外wheelCount宣告成private,其他類別繼承後也是看不到他,所以要改成 protected。

(7)

Given class defined in two different files 1. package util;

2. public class BitUtils{

3. private static void process(byte[] b){}

4. }

1. package app;

2. public class TestApp{

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

4. byte[] bytes = new byte[256];

5. // insert code here 6. }

7. }

What is required at line 5 in class TestApp to use the process method of BitUtils?

A. PROCESS(BYTES);

B. BitUtils.process(bytes);

C. app.BitUtils.process(bytes);

D. util.BitUtils.process(bytes);

E. import util.BitUtils.*; process(bytes);

F. TestApp cannot use the process method in BitUtils.

【答案】F

【參考章節】1-5-1 非存取的成員修飾子

【試題解析】

被設成private的含識或變數除自身類別外,其他類別皆無法存取(包含繼承)。

(8)

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:

1. package alpha;

2. 1. class Alpha{

3. 2. String alpha;

4. 3. Alpha(){ this(“A”); }

5. 4. Alpha(String a) { alpha = a; } 6. }

7. package beta;

8. 5. class Beta extends alpha.Alpha { 9. 6. Beta(String a) { super(a); } 10. }

Modifiers:

A. private B. protected C. public

1 2 3 4 5 6

(9)

1 2 3 4 5 6

C A C B C C

1. package alpha;

2. public class Alpha{

3. private String alpha;

4. public Alpha(){ this(“A”); }

5. protected Alpha(String a) { alpha = a; } 6. }

7. package beta;

8. public class Beta extends alpha.Alpha { 9. public Beta(String a) { super(a); } 10. }

【參考章節】1-5-1 存取修飾子

【試題解析】

第2、4、8、9行符合條件一。

第3行符合條件二。

第5行符合條件三。

(10)

Which three statements are true? (Choose three)

A. A final method in class X can be abstract if and only if X is abstract.

B. A protected method in class X can be overridden by any subclass of X.

C. A private static method can be called only within other static methods in class X.

D. A non-static public final method in class X can be overridden in any subclass of X.

E. A public static method in class X can be called by a subclass of X without explicitly referencing the class X.

F. A method with the same signature as a private final method in class X can be implemented in a subclass of X.

【答案】BEF

【參考章節】1-5-1 存取修飾子

【試題解析】

選項A:錯誤。在abstract class中不可以將abstract 方法修飾為final,abstract原本就 是要被繼承改變的,final是讓方法不得再被改變,所以加上 final 修飾沒有意義。

選項B:正確。子類別可以合法覆寫父類別中標示為protected的方法。

選項C:錯誤。private static 方法在 class X 中的其它static 方法可以呼叫之外,

non-static的方法也可以呼叫。

選項D:正確。子類別呼叫父類別的static的方法,可以不用明確的加上父類別參照。

選項F:正確。父類別的final方法的存取權限若是private,則子類別自己可以有著與 父類別一樣的private final的方法。此種情況不是覆寫,而是各自傭有同樣名稱的private final的方法。

(11)

Given the following Java code:

11. public class ItemTest{

12. private final int id ;

13. public ItemTest(int id) { this.id = id;}

14. public void updateId(int newId){ id = newId; } 15.

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

17. ItemTest fa = new ItemTest(42);

18. fa.updateId(69);

19. System.out.println(fa.id);

20. } 21. }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. The attribute id in the Item object remains unchanged.

D. The attribute id in the Item object is modified to the new value.

E. A new Item object is created with the preferred value in the id attribute.

【答案】A

【參考章節】1-5-4 變數的宣告

【試題解析】

Final修飾子在基本數值上為一經指定就不能修改其指定的數值,但本題在17行經 由”建構方法”宣告了id = 42,但因為在14行嘗試修改final id的數值,所以編譯無法通過。

注意:在實體變數中(instance variable),系統會自動給予基本數值與物件參考初始 值,但如果實體變數被標示為final,則編譯器將不會給予初始值0。但若經由建構方法 可以指定未經指派標示為final變數的值。

(12)

Given the following Java code:

11. public abstract class Shape{

12. int x;

13. int y;

14. public abstract void draw();

15. public void setAnchor(int x, int y){

16. this.x = x;

17. this.y = y;

18. } 19. }

And class Circle that extends and fully implements the Shape class. Which is correct?

A. Shape s = new Shape();

s.setAnchor(10,10);

s.draw();

B. Circle c = new Shape();

c.setAnchor(10,10);

c.draw();

C. Shape s = new Circle();

s.setAnchor(10,10);

s.draw();

D. Shape s = new Circle();

s->setAnchor(10,10);

s->draw();

E. Circle c = new Circle();

c.Shape.setAnchor(10,10);

c.Shape.draw();

【答案】C

【參考章節】1-5-2 非存取的成員修飾子

【試題解析】

選項A:錯誤。抽象類別不能利用new來建立實體(instance),new Shape()是錯誤的。

選項B:錯誤。錯誤原因與A相同。

選項C:正確。使用物件導向的多型技巧來建立Circle的實體。

選項D:錯誤。Java沒有這種運算子。

選項E:錯誤。語法錯誤,將c.Shape.setAnchor(10,10)與c.Shape.draw()。改為 c.setAnchor(10,10)與c.draw()。

(13)

Given the following Java code:

11. public abstract class Shape{

12. int x;

13. int y;

14. public abstract void draw();

15. public void setAnchor(int x, int y){

16. this.x = x;

17. this.y = y;

18. } 19. }

Which two classes use the Shape class correctly? (Choose two) A. public class Circle implements Shape{

private int radius;

}

B. public abstract class Circle extends Shape{

private int radius;

}

C. pubic class Circle extends Shape{

private int radius;

public void draw();

}

D. public abstract class Circle implements Shape{

private int radius;

public void draw();

}

E. public class Circle extends Shape{

private int radius;

public void draw(){/* code here */}}

F. public abstract class Circle implements Shape{

private int radius;

public void draw(){}

}

(14)

【參考章節】1-5-2 非存取的成員修飾子

【試題解析】

選項A:錯誤。abstract 的繼承是用extends 而不是implements 選項B:正確。abstract class可以不必實作所有abstract method

選項C:錯誤。未實作draw method,理由同上,相反的,non-abstract class必須實作 所有abstract method

選項D:錯誤。理由同選項A

選項E:正確。正確的繼承abstract class與實作 選項F:錯誤。理由同選項A

(15)

Replace two of the Modifiers that appear in the Single class to make the code compile.

Note: Three modifiers will not be used and four modifiers in the code will remain unchanged.

Code:

1. public class Single{

2. private static Single instance;

3. public static Single getInstance(){

4. if(instance == null) instance = create();

5. return instance;

6. }

7. private Single(){}

8. protected Single create(){ return new Single(); } 9. }

10. class SingleSub extends Single{

11. } Modifiers:

A. final B. protected C. private D. abstract E. static F. no change

12. public class Single{

13. 1. 2. Single instance;

14. 3. 4. Single getInstance(){

15. if(instance == null) instance = create();

16. return instance;

17. }

18. 5. Single(){}

19. 6. Single create(){ return new Single(); } 20. }

21. class SingleSub extends Single{

22. }

1 2 3 4 5 6

(16)

1 2 3 4 5 6

F F F F B E

1. public class Single{

2. private static Single instance;

3. public static Single getInstance(){

4. if(instance == null) instance = create();

5. return instance;

6. }

7. protected Single(){}

8. static Single create(){ return new Single(); } 9. }

10. class SingleSub extends Single{

11. }

【參考章節】1-5-4 變數的宣告

【試題解析】

private Single(),private會造成SingleSub繼承時找不到建構方法,依照題意修改成 protected。另外static方法無法直接存取non-static方法,所以本題只需將”protected Single create()”修改成”static Single create()”即可通過編譯。

(17)

A programmer needs to create a logging method that can accept an arbitrary number of arguments. For example, if may be called in these ways:

logIt(“log message1”);

logIt(“log message1”, “log message2”);

logIt(“log message1”, “log message2”, “log message3”);

Which declaration satisfies this requirement?

A. public void logIt(String * msgs) B. public void logIt(String [] msgs) C. public void logIt(String… msgs)

D. public void logIt(String msg1, String msg2, String msg3)

【答案】C

【參考章節】1-5-2 非存取的成員修飾子

【試題解析】

選項A:錯誤。沒有String * 這種宣告 選項B:錯誤。只能接受”String Array”

選項C:正確。可接受不定個同類型參數 選項D:錯誤。只能接受”固定”三個String參數

(18)

Given the following Java code:

1. public abstract class Shape{

2. private int x;

3. private int y;

4. public abstract void draw();

5. public void setAnchor( int x, int y ){

6. this.x = x;

7. this.y = y;

8. } 9. }

Which two classes use the Shape class correctly? (choose two) A. public class Circle implements Shape {

private int radius; }

B. public abstract class Circle extends Shape { private int radius; }

C. public class Circle extend Shape { private int radius;

public void draw(); }

D. public abstract class Circle implements Shape { private int radius;

public void draw(); }

E. public class Circle extends Shape { private int radius;

public void draw() { /* CODE HERE */ } } F. public abstract class Circle implements Shape { private int radius;

public void draw() { /* CODE HERE */ } }

【答案】BE

【參考章節】1-5-2 非存取的修飾子

【試題解析】

非抽象類別一定要實作所有抽象函式。

選項A:錯誤。繼承抽象類別需透過extends。

選項B:正確。抽象函式可以不必實作所有函式。

選項C:錯誤。沒有實作draw()。

選項D:錯誤。應使用extends來繼承類別。

選項E:正確。

選項F:錯誤。同D。

(19)

Given the following Java code:

1. public class Pizza{

2. java.util.ArrayList toppings;

3. public final void addTopping(String topping){

4. toppings.add(topping);

5. } 6. }

7. class PepperoniPizza extends Pizza{

8. public void addTopping(String topping){

9. System.out.println("Cannot and Uoppings");

10. }

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

12. Pizza pizza = new PepperoniPizza();

13. pizza.addTopping("Mushrooms");

14. } 15. }

What is the result?

A. Compilation fails.

B. Cannot add Toppings.

C. The code runs with no output.

D. A NullPointerException is thrown in Line 4.

【答案】A

【參考章節】1-5-2 非存取的修飾子

【試題解析】

PepperoniPizza 類別中第8行的addTopping()方法不能覆寫Pizza類別的final方法 addTopping(),因為被標示為final方法,代表是最後的方法,不可加以覆寫。

(20)

Given the following Java code:

1. class ClassA{

2. public int numberOfInstances;

3. protected ClassA(int numberOfInstances ){

4. this.numberOfInstances = numberOfInstances;

5. } 6. }

7. public class ExtendsA extends ClassA{

8. private ExtendsA (int numberOfInstances){

9. super(numberOfInstances);

10. }

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

12. ExtendsA ext = new ExtendsA (420);

13. System.out.println(ext.numberOfInstances);

14. } 15. }

Which statement is true?

A. 420 is the output.

B. An exception is thrown at runtime.

C. All constructors must be declared public.

D. Constructors CANNOT use the private modifier.

E. Constructors CANNOT use the protected modifier.

【答案】A

【參考章節】1-5-3 建構子的宣告

【試題解析】

雖然該類別建構子設定為private,但本題的new ExtendsA(420)是在同一個類別,可 以順利的編譯本題並輸出420。

(21)

Which of the following are the valid ways to define a constructor for class Test?(Choose two.) A. public void Test(){}

B. public Test(){}

C. private Test(){}

D. public static Test() E. final Test(){}

【答案】BC

【參考章節】1-5-3 建構子的宣告

【試題解析】

建構子的修飾子不可以是native, final, static, synchronized和abstract。

(22)

Given the following Java code:

55. int [] x = {1,2,3,4,5};

56. int y[] = x;

57. System.out.println(y[2]);

Which statement is true?

A. Line 57 will print the value 2.

B. Line 57 will print the value 3.

C. Compilation will fail because of an error in line 55.

D. Compilation will fail because of an error in line 56.

【答案】B

【參考章節】1-5-4 變數的宣告

【試題解析】

陣列的索引值都是從0開始,所以y[2]是第三個陣列元素”3”。

(23)

Given the following Java code:

enum Example { ONE, TWO, THREE };

Which statement is true?

A. The expressions (ONE == ONE) and ONE.equals(ONE) are both guaranteed to be true.

B. The expression (ONE < TWO) is guaranteed to be true and ONE.compareTo(TWO) is guaranteed to be less than one.

C. The Example values cannot be used in a raw java.util.HashMap.; instead, the programmer must use a java.util.EnumMap.

D. The Example values can be used in a java.util.sSortedSet, but the set will NOT be sorted because enumerated Type do not implement java.lang.Comparable.

【答案】A

【參考章節】4-1-2 關係運算子

【試題解析】

選項A: 兩個都是 ONE, == 與 equals() 為 true 選項B: ONE.compareTo(TWO) 為 -1

選項C: 這個 enum Example 是可以放進 HashMap 中使用沒有問題.

選項D: 當然也可以放進 SortedSet

(24)

Given the following Java code:

1. class Test{

2. public enum Direction { NORTH, SOUTH, EAST, WEST } 3. }

4. public class Sprite{

5. // insert code here 6. }

Which code, inserted at line 14, allows the Sprite class to compile?

A. Direction d = NORTH;

B. Test.Direction d = NORTH;

C. Direction d = Direction.NORTH;

D. Test.Direction d = Test.Direction.NORTH;

【答案】D

【參考章節】1-5-5 Enum的宣告

【試題解析】

原則上,Test中的enum列舉Direction 就是Test的內部類別,因此可用內部類別的觀 點去存取它。要取得Direction必須經由Test這個類別才行,而enum中的元素都是static成 員,所以選擇選項D。

(25)

Drag and drop the appropriate code excerpts in the empty boxes provided in the code, so that the code compiles without errors.

enum Fish{

1. ;

2. 3. ; 4. Fish(int price) { this.price = price

}

public String getDescription() { switch( 5. ) {

case GOLDFISH : return “Gold fish priced at ” + price;

case ANGELFISH : return “Angel fish priced at ” + price;

case GUPPY : return “Fancy fish priced at ” + price;

default : return “No such fish”;

} } }

A this B this.value C int price D void

E private F public G GOLDFISH(7),ANGELFISH(5),GUPPY(2)

1. 2. 3. 4. 5.

(26)

1. 2. 3. 4. 5.

G E C E A

enum Fish{

GOLDFISH(7), ANGELFISH(5), GUPPY(2) ; private int price ;

private Fish(int price) { this.price = price

}

public String getDescription() { switch( this ) {

case GOLDFISH : return “Gold fish priced at ” + price;

case ANGELFISH : return “Angel fish priced at ” + price;

case GUPPY : return “Fancy fish priced at ” + price;

default : return “No such fish”;

} } }

【參考章節】1-5-5 Enum的宣告 5-1-2 switch 陳述式

【試題解析】

enum 元素無修飾字 static、final、abstract、protected 或 private。

enum 元素可以有參數,所以第一個為元素列。

建構子中提及 this.price,表示有 price 屬性成員,只被建構子存取,且為 int,因 此為 private int price。

enum 建構子一定不能被其他類別所呼叫,所以 private。

switch 中的 case 為其元素,所以 switch 測試項為本 enum。

(27)

Place the lines in the correct order to complete the enum.

enum Element{

1.

2.

3.

4.

5.

Lines

A. FIRE{ public String info() { return “Hot”; } B. public String info() { return “elements”; } C. } D. };

E. EARTH, WIND,

1 2 3 4 5

【答案】

1 2 3 4 5

E A D B C

enum Element{

EARTH, WIND, FIRE{ public String info() { return “Hot”; } };

public String info() { return “elements”; } }

【參考章節】1-5-5 Enum的宣告

【試題解析】

Enum常數後面可以加上”常數特定類別本體”(constant specific class body)以實作 enum常數特有的方法,但常數特定類別本體必須以”};”作為結束。

(28)

Given:

2. public class Test{

3. public enum Dogs { collie, harrier };

4. public static void main(String [] args) { 5. Dogs myDog = Dogs.collie;

6. switch (myDog) { 7. case collie:

8. System.out.println(“collie”);

9. case harrier:

10. System.out.println(“harrier”);

11. } 12. } 13. }

What is the result?

A. collie B. harrier

C. Compilation fails.

D. collie harriers

E. An exception is thrown at runtime.

【答案】D

【參考章節】1-5-5 Enum的宣告 5-1-2 switch 陳述式

【試題解析】

第五行將myDog指向Dogs.collie,第六行的swith可接收myDog參數執行case collie:,但因為switch裡面沒有加上break;,所以當印完collie接著會執行下一個case harrier:印出harrier。

(29)

Given the following Java code:

10. public enum Title{

11. MR(“Mr.”), MRS(“Mrs.”), MS(“Ms.”);

12. private final String title;

13. private Title(String t){title = t;}

14. public String format(String last, String first){

15. return title + “ ” + first + “” + last;

16. } 17. }

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

19. System.out.println(Title.MR.format(“Doe”,”John”));

20. }

What is the result?

A. Mr. John Doe

B. An exception is thrown at runtime.

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

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

E. Compilation fails because of an error in line 20.

【答案】A

【參考章節】1-5-5 Enum的宣告。

【試題解析】

第21行會呼叫MR(“Mr.”)完成建構子後再執行format(“Doe”,”John”)回傳字串,接著 印出Mr. John Doe。

(30)

Given the following Java code::

11. public class Rainbow { 12. public enum MyColor {

13. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff);

14. private final int rgb;

15. MyColor(int rgb){this.rgb = rgb;}

16. public int getRGB(){return rgb;}

17. }

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

19. //insert code here 20. }

21. }

Which code fragment inserted at line 19, allows the Rainbow class to compile?

A. MyColor skyColor = BLUE;

B. MyColor treeColor = MyColor.GREEN;

C. if(RED.getRGB() < BLUE.getRGB()){}

D. Compilation fails due to other error(s) in the code.

E. MyColor purple = new MyColor(0xff00ff);

F. MyColor purple = MyColor.BLUE + MyColor.RED;

【答案】B

【參考章節】1-5-5 Enum的宣告。

【試題解析】

選項A:錯誤。必須以類別的觀點來存取函式。

選項B:正確。

選項C:錯誤。必須以類別的觀點來存取函式。

選項D:錯誤。並非全都是錯的。

選項E:錯誤。不能直接呼叫Enum的建構子。

選項F:錯誤。Enum元素不能使用 + 運算子。

(31)

Given the following Java code:

11. public class Test {

12. public enum Dogs { collie, harrier, shepherd } 13. public static void main( String[ ] args ) { 14. Dogs myDog = Dogs.shepherd;

15. switch (myDog) { 16. case collie:

17. System.out.print( “collie “);

18. case default:

19. System.out.print( “retriever “);

20. case harrier:

21. System.out.print( “harrier “);

22. } 23. } 24. }

What is the result?

A. harrier B. shepherd C. retriever

D. Compilation fails E. retriever harrier

F. An exception is thrown at runtime.

【答案】D

【參考章節】1-5-5 Enum的宣告。

【試題解析】

列舉的元素可以當成switch的比對子,本題編譯錯誤,是因為在switch-case的敘述中 並沒有程式第18行case default這樣的語法,請改成default。

本題比對子case或default(若不是放在最後面)沒有加上break,所以在利用java –Xlint 編譯時會產生警告訊息。

(32)

Given the following Java code:

10. package test;

11.

12. class Target {

13. public String name = “hello”

14. }

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

【答案】C

【參考章節】2-1 封裝(Encapsulation)

【試題解析】

Target類別沒宣告任何存取修飾子,所以其存取權限為Default,只有同package裡面 的類別才能存取該類別。

(33)

Given the following Java code:

1. public class JavaCard{

2.

3. private String cardID;

4. private Integer limit;

5. public String ownerName;

6.

7. public void setCardInformation ( String cardID, 8. String ownerName, 9. Integer limit) { 10. this.cardID = cardID;

11. this.ownerName = ownerName;

12. this.limit = limit;

13. } 14. }

Which statements is true?

A. The class is fully encapsulated.

B. The code demonstrates polymorphism.

C. The ownerName variable breaks encapsulation.

D. The cardID and limit variables break polymorphism.

E. The setCardInformation method breaks encapsulation.

【答案】C

【參考章節】2-1 封裝(Encapsulation)

【試題解析】

依據封裝的特性,為了避免該類別的實體變數被直接取用,應該將 ownerName 的 存取權限修改為”private”。

(34)

Which two statements are true? (Choose two) A. An encapsulated, public class promotes re-use.

B. Classes that share the same interface are always tightly encapsulated.

C. An encapsulated class allows subclasses to overload methods, but does NOT allow overriding methods.

D. An encapsulated class allows a programmer to change an implementation without affecting outside code.

【答案】AD

【參考章節】2-1 封裝(Encapsulation)

【試題解析】

選項 A:正確。被設為 public 的類別應該被重複的使用。以促進物件導向的程式設 計。

選項 B:錯誤。實作同樣介面的類別不必一定得封裝在一起。

選項 C:錯誤。封裝允許使用者去多載及覆寫它的方法。

選項 D:正確。一個好的封裝可以讓設計者改變它的實作方式時,不會去改變外部 呼叫該類別的程式碼。

(35)

Which two statements are true about has-a and is-a relationships? (Choose two) A. Inheritance represents an is-a relationship.

B. Inheritance represents a has-a relationship.

C. Interfaces must be used when creating a has-a relationship.

D. Instance variables can be used when creating a has-a relationship.

【答案】AD

【參考章節】2-2 繼承、Is-A、Has-A

【試題解析】

Is-a:在字面上是「是一個」的意思,屬上下的關係,在 Java 與眼中是利用 extends 的關鍵字來延伸類別,也就是繼承的關係,因此選項 A 是正確的。例如:電腦是電子機 械產品的一種,電腦這個類別繼承了電子機械產品的類別,所以電子機械產品就成了電 腦的父類別,而電腦就成了電子機械產品的子類別。

Has-a:在字面上是「有一個」的意思,屬聚合的關係,是用來表示類別中的成員 變數。 例如:電腦中有一個 CPU、1G 的 RAM、500GB HD…等,CPU、RAM 與 HD 變成了電腦的成員變數(Instance variables)。

選項 A:正確。繼承使得類別成為 Is-a 的關係。

選項 B:錯誤。同上。

選項 C:錯誤。介面為無法直接實作的方法,所以沒必要也沒辦法去使用介面實體。

選項 D:正確。當類別 A 與類別 B 有了 A has-a B 的關係,則 A 可以使用 B 類別的 實體。

(36)

Place the Types in one of the Type columns, and the Relationships in the Relationship column, to define appropriate has-a and is-a relationships.

Type Relationship Type

1. 2. Animal

Forest 3. 4.

Rectangle 5. 6.

7. 8. Programming Book

Code:

Relationships Types

A. is-a C. Dog

B. has-a D. Side

E. Tail F. Square G. Tree H. Book I. JavaBook J. Pen

1. 2. 3. 4. 5. 6. 7. 8.

【答案】

1. 2. 3. 4. 5. 6. 7. 8.

C A B G B D I A

Type Relationship Type

Dog is-a Animal

Forest has-a Tree

Rectangle has-a Side

Java Book is-a Programming Book

【參考章節】2-2 繼承、Is-A、Has-A

【試題解析】

狗是一種動物。

森林包含了樹。

長方形有邊。

Java 書是一種程式設計書。

(37)

Look the picture.

Place the Relations on their corresponding implementation Structures.

Note:Not all implementation Structures will be used.

Implementation Structures Relations

1.

class A{

List<B> b;

}

2.

class A{

B b; C c;

}

A.

Car is a Vehicle And

Car is a Collectible

3. Class A{} 4.

class A{

B b;

}

B. Car has a

SteeringWheel

5. class A

extends B{} 6.

class A

implements B, C { }

C. Car has Wheels

7. class A

extends B, C{ } D. Mini is a Car

E. Car is an Object

F null

1. 2. 3. 4. 5. 6. 7.

(38)

1. 2. 3. 4. 5. 6. 7.

C F E B D A F

Place the Relations on their corresponding implementation Structures.

Note:Not all implementation Structures will be used.

Implementation Structures Relations

Car has Wheels

class A{

B b; C c;

}

A.

Car is a Vehicle And

Car is a Collectible

Car is an Object Car has a

SteeringWheel B. Car has a

SteeringWheel

Mini is a Car

Car is a Vehicle And

Car is a Collectible

C. Car has Wheels

class A

extends B, C{ } D. Mini is a Car

E. Car is an Object

【參考章節】2-2 繼承、Is-A、Has-A

【試題解析】

class Car

implements Vehicle, Collectable{ } class Car {

SteeringWheel sw; } class Car {

List<Wheel> wheels; } class Mini

extends Car{ }

class Car {} //任何類別都會自動繼承 Object。

(39)

Given the following Java code:

10. interface Jumper { public void jump(); }

20. class Animal {}

30. class Dog extends Animal { 31. Tail tail;

32. }

40. class Beagle extends Dog implements Jumper{

41. public void jump() { } 42. }

50. class Cat implements Jumper{

51. public void jump() { } 52. }

Which three are true? (Choose three.) A. Cat is-a Animal.

B. Cat is-a Jumper.

C. Dog is-a Animal.

D. Dog is-a Jumper.

E. Cat has-a Animal.

F. Beagle has-a Tail.

G. Beagle has-a Jumper.

【答案】BCF

【參考章節】2-2 繼承、Is-A、Has-A

【試題解析】

選項A:錯誤。Cat 並沒有 extends Animal。

選項B:正確。

選項C:正確。

選項D:錯誤。Dog 並沒有 implements Jumper。

選項E:錯誤。Cat 內沒宣告(或透過繼承擁有)Animal型別的參考。

選項F:正確。

選項G:錯誤。理由同選項E。

(40)

Given the following Java code:

10. class A{

11. public void process(){ System.out.println(“A, ”); }}

12. class B{

13. public void process() throws IOException{

14. super.process();

15. System.out.print(“B, ”);

16. throw new IOException();

17. }

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

19. try{ new B().process(); }

20. catch(IOException e){ System.out.println(”Exception”); }}}

What is the result?

A. Exception.

B. A, B, Exception

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

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

E. A NullPointerException is thrown at runtime.

【答案】D

【參考章節】2-2 繼承、Is-A、Has-A

【試題解析】

第14行會造成編譯錯誤,因為B的隱含父類別Object並不提供process()函式。有興趣 的可以上java.sun.com上去查Object的API有提供哪些函式。

(41)

Which Man class properly represents the relationship “Man has a best friend who is a Dog”?

A. class Man extends Dog{}

B. class Man implements Dog{}

C. class Man { private BestFriend dog ;}

D. class Man { private Dog bestFriend;}

E. class Man { private Dog;}

F. class Man { private BestFriend;}

【答案】D

【參考章節】2-2-2 HAS-A

【試題解析】

選項A:人類是狗 選項B:人類是狗

選項C:人類有個叫”狗”的好朋友。

選項D:人類有個好朋友就是狗 選項E:錯誤的宣告。

選項F:錯誤的宣告。

(42)

Given the following Java code:

10. interface A { public int getValue(); } 11. class B implements A {

12. public int getValue() { return 1; } 13. }

14. class C extends B { 15. // insert code here 16. }

What three code fragments inserted individually at line 15, make used of polymorphism?

(Choose three)

A. public void add(C c) { c.getValue(); } B. public void add(B b) { b.getValue(); } C. public void add(A a) { a.getValue(); } D. public void add(A a, B b) { a.getValue(); } E. public void add(C c1, C c2) { c1.getValue(); }

【答案】BCD

【參考章節】2-3 多型

【試題解析】

選項 BCD 都有多型的特徵,但選項A與E只是單純的在class C的環境下來存取自身 的物件。

例如在C物件中,要呼叫add(B b),可能必須先利用多型建立一個物件B b = new C();,再透過b物件變數(實際上是C的物件實體) 利用pass-by-value的機制傳遞給add(B b),或是B b = new B();(實際上是B的物件實體)利用pass-by-value的機制來傳遞給add(B b)。

就add(C c)而言,先前也許只需要建立C c = new C(),再利用pass-by-value的機制傳 遞給add(C c)。

(43)

Place the Output Options in the Actual Output Sequence to indicate the output from this code:

1. class Alpha {

2. public void foo(String… args) { 3. System.out.println(“Alpha:foo”);

4. }

5. public void bar(String a){

6. System.out.println(“Alpha:bar”);

7. } 8. }

9. public class Beta extends Alpha { 10. public void foo(String a){

11. System.out.println(“Beta:foo”);

12. }

13. public void bar(String a){

14. System.out.println(“Beta:bar”);

15. }

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

17. Alpha a = new Beta();

18. Beta b = (Beta)a;

19. a.foo(“test”);

20. b.foo(“test”);

21. a.bar(“test”);

22. b.bar(“test”);

23. } 24. }

Actual Output Sequence:

1 2 3 4 Out Options:

A. Alpha:foo B. Alpha:bar C. Beta:foo D. Beta:bar

1 2 3 4

(44)

1 2 3 4

A C D D

Alpha:foo Beta:foo Beta:bar Beta:bar

【參考章節】2-3 多型

【試題解析】

在Alpha a觀點只知道以下兩個方法:

public void foo(String… args) public void bar(String a)

在Beta b觀點知道以下四個方法:

public void foo(String a) //自己的方法 public void bar(String a) //覆寫

public void foo(String… args) //在Alpha定義的 public void bar(String a) //在Alpha定義的

程式第20行在這裡可以想成Beta b = new Beta()。因此很自然地就執行到程式第10 行的foo()方法,印出”Beta:foo”。

執行第21行,在a的觀點中,因為要執行bar(“test”)時有public void bar(String a)可以 呼叫,而在new Beta()實作中接到了呼叫public void bar(String a)的需求,因而執行程式第 13~15行並印出”Beta:bar”,這是覆寫機制。

執行第22行,執行到程式第13行的bar(String a)方法因而印出”Beta:bar”。

(45)

Given the following Java code:

1. class A{

2. String name = “A”;

3. String getName() { 4. return name;

5. }

6. String greeting() { 7. return “class A”;

8. } 9. }

10. class B extends A { 11. String name = “B”;

12. String greeting() { 13. return “class B”;

14. } 15. }

16. public class Client {

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

18. A a = new A();

19. A b = new B();

20. System.out.println(a.greeting() + “ has name ” + a.getName());

21. System.out.println(b.greeting() + “ has name ” + b.getName());

22. } 23. }

Place the names “A” and “B” in the following output.

class 1. has name 2.

class 3. has name 4.

Names

A. A B. B

1. 2. 3. 4.

(46)

1 2 3 4

A A B A

class A has name A class B has name A

【參考章節】2-3 多型

【試題解析】

創建一個有繼承關係的子類別,將會分別產生子類別實體與父類別的實體。

a.greeting() – 很簡單的就是單純的A型別的參考,存取A實體的方法。

a.getName() – 同上,單純的存取A實體的方法。

b.greeting() – A型別參考b存取B實體,由於B類別覆寫了A類別的greeting方法,所以 此行是執行B類別的greeting方法,印出”B”。

b.getName() - A型別參考b存取B實體,在此行因為B類別並無覆寫A類別的getName 方法,所以會指向A實體中的getName方法,所以印出的name自然而然的就是A實體 的”A”。

(47)

Given the following Java code:

10. interface A { 11. void x();

12. }

13. class B implements A { 14. public void x() {}

15. public void y() {}

16. }

17. class C extends B { 18. public void x(){}

19. } And:

20. java.util.List<A> list = new java.util.ArrayList<A>();

21. list.add(new B());

22. list.add(new B());

23. for(A a : list) { 24. a.x();

25. a.y();

26. }

What is the result?

A. The code runs with no output.

B. An exception is thrown at runtime.

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

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

E. Compilation fails because of an error in line 23.

F. Compilation fails because of an error in line 25.

【答案】F

【參考章節】2-3 多型

【試題解析】

由於編譯時期編譯器所參考的是參考變數的型別,所以在執行第25行時,會因為介 面A中並沒有宣告y方法,除非將25進行強制向下轉型,不然執行到程式第25行會產生編 譯時期錯誤。

(48)

Given:

10. abstract class A { 11. abstract void a1();

12. void a2() {}

13. }

14. class B extends A { 15. void a1() {}

16. void a2() {}

17. }

18. class C extends B { void c1() {} }

And:

A x = new B(); C y = new C(); A z = new C();

What are four valid examples of polymorphic method calls? (Choose four.) A. x.a2();

B. z.a2();

C. z.c1();

D. z.a1();

E. y.c1();

F. x.a1();

【答案】ABDF

【參考章節】2-3 多型

【試題解析】

選項ABDF都成功使用多型來存取方法。

選項C錯誤,因為在編譯時期,編譯器會判斷出參考變數z所指向的A類別並沒有c1 的方法。

選項E錯誤,因為它沒使用多型的方式,只是一般的方法存取。

(49)

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 racar = new RaceCar();

22. Car car = new RaceCar ();

23. Vehicle vehicle = new RaceCar ();

24. System.out.println( racar.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.

【答案】D

【參考章節】2-3 多型

【試題解析】

本題由於執行的實體是RaceCar,而且RaceCar都有覆寫speed(),所以無論呼叫的參 考型別為繼承樹上的任一類別,都將呼叫RaceCar實體中的speed(),所以答案為D。

(50)

Given the following Java code:

1. public class Base {

2. public static final String FOO = “foo”;

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

4. Base b = new Base();

5. Sub s = new Sub();

6. System.out.println(Base.FOO);

7. System.out.println(Sub.FOO);

8. System.out.println(b.FOO);

9. System.out.println(s.FOO);

10. System.out.println(((Base)s).FOO);

11. } 12. }

13. class Sub extends Base{

14. public static final String FOO = “bar”;

15. }

What is the result?

A. foofoofoofoofoo B. foobarfoobarbar C. foobarfoofoofoo D. foobarfoobarfoo E. barbarbarbarbar F. foofoofoobarbar G. foofoofoobarfoo

【答案】D

【參考章節】2-3 多型

【試題解析】

繼承關係類別中,如果子類別命名了相同名字的實體變數(Instance variable),參考 的型別會影響到存取到哪個類別的實體變數。至於第6.7行沒透過實體就可以直接存取是 因為該實體變數宣告成static。

(51)

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:

A. private void bar( int x ) {}

B. public void bar( int x ) {}

C. public int bar( String x ) { return 1; } D. public Alpha bar( int x ) {}

E. public void bar( int x, int y ) {}

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

1. 2. 3.

(52)

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; } 錯誤。覆寫時的傳回值可以與原方法相等或是原方 法的直系子類別。

(53)

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:正確。同上。

(54)

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的,在覆寫父類別方法時,將會出現 錯誤!

(55)

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的覆寫皆是正確 的。

(56)

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:正確。

(57)

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來 修飾方法例外。

(58)

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 多載函式

【試題解析】

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

(59)

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的第六行,覆寫函式必須是原來函式的回傳型別或則是它的子型別。

(60)

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。

(61)

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。

(62)

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

(63)

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[]物件的內容值印出。

(64)

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”。

(65)

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:正確。

(66)

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。

參考文獻

相關文件

淘汰賽試題: 大蛋糕製作及裝飾 Entremets6.

1.本次分區競賽試題為公開試題。(試題內容將不做任何百分之三十的調整) 2.參加競賽選手應於競賽開始前 30

第二十四條 學、術科測 試辦理單位應遴聘具有 下列資格之一者,擔任 學科測試及術科測試採 筆試非測驗題方式之監 場人員:. 一、

(一)初試:採筆試方式,題目類型為選擇題,每科目題數各 50 題(每題 2 分,各題未作 答不予計分,答錯倒扣 0.6 分) 。初試成績達參加複試標準(初試科目其中

七、 應試者對於試題若有疑義,應於甄試 結束次日起三個工作日內、以及對於

題目問什麼?請把它找出來。 【題目問共要 花多少錢】.. 換你試試看 換你試試看

(以下簡稱「99 課綱微調」)命題 1 。本考試說明即針對實施 99 課綱微調後,施測之化學 科,說明命題方向與

(一)NVDA 電子試題 WORD