11~15題出現比較多JAVA特有的語法
也有一些物件的概念,遇到不清楚的地方一定要全面的了解
第十一題:
|
Given:
class Alligator { public
static
void main(String[] args) { int[] x[] = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
int y[][] = x; System.out.print(y[2][1]); } }
|
What is the result?
A. 2 B. 3 C. 4 D. 6 E. 7 F. Compilation fails.
|
答案:E
|
執行結果:答案中
|
題目範圍:基本概念
|
解析:
十分罕見的用法int[] x[] 意思等同於int x[][] 和int[][] x
雖然JAVA沒有指標的語法,不過還是隱藏了指標的觀念 在int y[][] = x;這行指令,就是把x 的指標傳給y
|
第十二題(拖拉題):
|
Add methods 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 { Place here public void bar(int x){} Place here public int bar(String x){return 1;} Place here public void bar(int x, int y){}
}
Methods
private void bar(int x){}
|
public void bar(int x){}
|
public int bar(String x){return 1;}
|
public Alpha bar(int x){}
|
public void bar(int x, int y){}
|
public int bar(int x){return x;}
|
|
答案:題目反白,順序無關
|
執行結果:無
|
題目範圍:物件觀念
|
解析:
繼承的觀念,所繼承的方法可見度只能增大不可縮小 private→(default)→protected→public
繼承來的方法,可以做多載(overLoading),但不可以覆蓋為不同的回傳型態 overLoading:public
void bar(int x){},public
void bar(int x, int y){} 不同的回傳型態:public
void bar(int x){},public
int bar(int x){}←錯誤
類題:二十八題
|
第十三題:
|
Given:
public
class Barn { public
static
void main(String[] args) { new Barn.go("hi", 1); new Barn.go("hi", "world", 2); }
public
void go(String... y, int x) { System.out.print(y[y.length - 1] + " "); } }
|
What is the result?
A. hi hi B. hi world C. world world D. Compilation fails. E. An exception is thrown at runtime.
|
答案:D
|
執行結果:答案中
|
題目範圍:基本概念
|
解析:
這題有兩個錯誤的地方,都會造成編譯錯誤,算是很仁慈的題目 第一個錯誤是在new Barn.go("hi", 1);產生物件同時使用物件的方法,產生物件的語法錯了 應修改為new Barn().go("hi", 1); new Barn.go("hi", 1);這樣的用法意思是產生一個Barn的InnerClass go 的物件
另一個錯誤的地方是在多變數方法的表示錯誤:public
void go(String... y, int x) {} 這個用法表示在傳入參數時,可以傳入一個以上的String 變數,這些變數會被放在y 陣列中 不過這樣的用法只能單獨使用,或是放在最末端:(int x, String... y),否則就會出現編譯錯誤
|
第十四題:
|
Given:
class Person { String name = "No name";
public Person(String nm) {name = nm;} }
class Employee extends Person { String empID = "0000";
public Employee(String id) {empID = id;} }
class EmployeeTest { public
static
void main(String[] args) { Employee e = new Employee("4321"); System.out.println(e.empID); } }
|
What is the result?
A. 4321
B. 0000
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 18.
|
答案:D
|
執行結果:答案中
|
題目範圍:物件觀念
|
解析:
第18行就是public Employee(String id) {empID = id;} 由於繼承的關係,子代類別被實體化成為物件時,父代的建構子會先被運行 預設運行的父代建構子是無引數的建構子super(),但是Person 類別沒有無引數的建構子,當明確寫出有引述的建構子時,無引數的建構子就會被取消掉,除非無引數的建構子也被明確寫出 這題要修正的話,有兩種方式: 1.在Employee的建構子的第一行加入super("name"); 2.在Person類別中加入一個無引數的建構子Person(){}
|
第十五題:
|
Given:
11. class Mud{ 12. //insert code here 13. System.out.println("hi"); 14. } 15. }
And the following five fragments:
public static void main(String...a){ public static void main(String.* a){ public static void main(String... a){ public static void main(String[]... a){ public static void main(String...[] a){
|
How many of the code fragments, inserted independently at line 12, compile?
A. 0 B. 1 C. 2 D. 3 E. 4 F. 5
|
答案:D
|
執行結果:hi
|
題目範圍:基本概念
|
解析:
方法的參數傳遞,多變數方法的使用 public static void main(String...a){ 正確的使用,這裡的a 是一個字串陣列 public static void main(String.* a){ 不可能有這種用法 public static void main(String... a){ 與第一種相同,...兩側插入空格不會影響 public static void main(String[]... a){ 這樣一來,就變成了多個陣列的傳入,而a 就會是二維的字串陣列 public static void main(String...[] a){ 不可以這樣使用
|
留言列表