第五十六題:

Given:

5. class Building{}
6. public class Barn extends Building{
7.     public static void main(String[] args){
8.     Building build1 = new Building();
9.     Barn barn1 = new Barn();
10.     Barn barn2 = (Barn)build1;
11.     Object obj1 = (Object)build1;
12.     String str1 = (String)build1;
13.     Building build2 = (Building)barn1;
14.     }
15. }

Which is true?

A. if line 10 is removed, the compilation succeeds.
B. if line 11 is removed, the compilation succeeds.
C. if line 12 is removed, the compilation succeeds.
D. if line 13 is removed, the compilation succeeds.
E. More than one line must be removed for compilation to succeed.

答案:

題目範圍:物件觀念、強制型態轉換

解析:

從子代轉為父代可以不需要強制型態轉換,例如筆電是個計算機
從父代轉換為子代需要強制轉換,例如我肯定這個計算機是台筆電
如果物件沒有直屬關係,無法轉換,例如筆電不會是桌上型電腦

這題的強制型態轉換即使成功通過編譯,但是執行時依然會出現錯誤

Exception in thread "main" java.lang.ClassCastException: Building cannot be cast to Barn at Barn.main

 

第五十七題:

Given:

21. class Money{
22.     private String country = "Canada";
23.     public String getC(){return country;}
24. }
25. class Yen extends Money{
26.     public String getC(){return super.country;}
27. }
28. public class Euro extends Money{
29.     public String getC(){return super.getC();}
30.     public static void main(String[] args){
31.         System.out.print(new Yen().getC() + " " + new Euro().getC());
32.     }
33. }

What is the result?

A. Canada
B. null Canada
C. Canada null
D. Canada Canada
E. Compilation fails due to an error on line 26.
F. Compilation fails due to an error on line 29.

答案:

題目範圍:物件觀念

解析:

如果變數被標記為private,即使用super去呼叫也不行
唯一的方法就是透過getter()來使用

這題如果將變數的private拿掉,執行結果會是Canada Canada

 

第五十八題:

Given:

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;

答案:

題目範圍:物件觀念

解析:
子代轉型為父代稱作向上轉型(UpCasting);父代轉為子代稱作向下轉型(DownCasting)
向上轉型是允許的,而且無須強制型態轉換
向下轉型必須強制轉換,而且即使因此通過編譯,也可能會出現ClassCastException

如果直接把父代的物件轉型為子代的物件,這樣就會出現錯誤
如果把子代的物件轉成父代,之後再強制轉回子代,這樣才是正確的

要確認物件是否允許轉換為某型態,可以用instanceof 關鍵字來判斷
instanceof直接翻譯為中文,可解釋為:是否為某類別的實體(物件)

 

第五十九題:

Given the following directory structure:

bigProject
|--source
| |--Utils.java
|
|--classes
|

And the following command line invocation:

javac d classes source/Utils.java

Assume the current directory is bigProject, what it the result?

A. If the compile is successful, Utils.class is added to the source directory.
B. The compiler returns an invalid flag error.
C. If the compile is successful, Utils.class is added to the classes directory.
D. If the compile is successful, Utils.class is added to the bigProject directory.

答案:

題目範圍:編譯指令

解析:

javac d 這個指令是指定要放編譯後檔案的目錄,用法如下:
javac
d [目的目錄] [來源檔案]

 

第六十題:

Given:

1. package com.company.application;
2.
3. public class MainClass{
4.      public static void main(String[] args){}
5. }

And MainClass exists in the/apps/com/company/application directory.
Assume the CLASSPATH environment variable is set to "." (current directory).

Which two java commands entered at the command line will run MainClass? (Choose two.)

A. java MainClass if run from the /apps directory

B. java com.company.application.MainClass if run from the /apps directory

C. java -classpath /apps com.company.application.MainClass if run from any directory

D. java-classpath . MainClass if run from the /apps/com/company/application directory

E. java -classpath /apps/com/company/application:. MainClass if run from the /apps directory

F. java com.company.application.MainClass if run from the /apps/com/company/application directory

答案:BC

題目範圍:編譯指令

解析:

執行時,路徑的使用方法

可以從任意地方指定絕對路徑,或是已經在路徑上,指定相對位置

arrow
arrow

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