第106題:

Given:

1. package util;
2. public class BitUtils{
3.     public static void process(byte[] b){/* more code here */}
4. }


1. package app;
2. public class SomeApp{
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 SomeApp to use the process method of BitUtils?

A. process(bytes);

B. BitUtils.process(bytes);

C. util.BitUtils.process(bytes);

D. SomeApp cannot use methods in BitUtils.

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

答案:

題目範圍:靜態方法

解析:

如果要直接使用別的package中的靜態方法,可以這樣使用

 

第107題:

Given:

10. class Inner{
11.     private int x;
12.     public void setX(int x){this.x = x;}
13.     public int getX(){return x;}
14. }
15.
16. class Outer{
17.     private Inner y;
18.     public void setY(Inner y){this.y = y;}
19.     public Inner getY(){return y;}
20. }
21.
22. public class Gamma{
23.     public static void main(String[] args){
24.         Outer o = new Outer();
25.         Inner i = new Inner();
26.         int n = 10;
27.         i.setX(n);
28.         o.setY(i);
29.         //insert code here
30.         System.out.println(o.getY().getX());

31.     }
32. }

Which three code fragments, added individually at line 29, produce the output 100? (Choose three.)

A. n = 100;

B. i.setX(100);

C. o.getY().setX(100);

D. i = new Inner(); i.setX(100);

E. o.setY(i); i = new Inner(); i.setX(100);

F. i = new Inner(); i.setX(100); o.setY(i);

答案:BCF

題目範圍:物件觀念與指標

解析:

這個題目要有些指標觀念才容易弄清楚

A:把n改成100n只是值,修改值並不會造成Inner的改變,因為i.setX(n);的時候丟進去的10是不會改變的

B:修改i 裡面的值,也會對o 造成影響,因為i 本身是物件,在o.setY(i);時,丟進去的是i 的指標

C:這個動作與B是完全一樣的

D:i = new Inner();的時候,i 就已經不是原本的i 了,所以這時候去修改i 裡面的值是沒有意義的

E:理由與D一樣

F:新實體化出一個i 並且重新與o 做連結,然後再修改裡面的值

 

第108題:

Given:

11. class Snoochy {
12.     Boochy booch;
13.     public Snoochy(){booch = new Boochy(this);}
14. }
15.
16. class Boochy{
17.     Snoochy snooch;
18.     public Boochy(Snoochy s){snooch = s;}
19. }

And the statements.

21. public static void main(String[] args){
22.     Snoochy snoog = new Snoochy();
23.     snoog = null;
24.     //more code here
25. }

Which statement is true about the objects referenced by snoog, snooch, and booch immediately after line 23 executes?

A. None of these objects are eligible for garbage collection.

B. Only the object referenced by booch is eligible for garbage collection.

C. Only the object referenced by snoog is eligible for garbage collection.

D. Only the object referenced by snooch is eligible for garbage collection.

E. The objects referenced by snooch and booch are eligible for garbage collection.

答案:

題目範圍:garbage collection 資源回收

解析:

斷頭了,下面的都死了

booch是在snoog裡面的;snooch是在booch裡面的,snoog的指標失效的話,全部都可以被回收了

 

第109題:

Given:

5. class Payload{
6.     private int weight;
7.     public Payload (int w){weight = w;}
8.     public void setWeight(int w){weight = w;}
9.     public String toString(){return Integer.toString(weight);}
10. }
11. public class TestPayload{
12.     static void changePayload(Payload p){/* insert code */}
13.     public static void main(String[] args){
14.         Payload p = new Payload(200);
15.         p.setWeight(1024);
16.         changePayload(p);
17.         System.out.println("p is " + p);
18. }}

Which code fragment, inserted at the end of line 12, produces the output p is 420?

A. p.setWeight(420);

B. p.changePayload(420);

C. p = new Payload(420);

D. Payload.setWeight(420);

E. p = Payload.setWeight(420);

答案:

題目範圍:物件觀念與指標

解析:

p是一個物件(的指標),當它被送進changePayload方法時,該方法裡面的p指的還是原來的那一個

 

第110題:

Given:

11. public static void test(Sting str){
12.     int check = 4;
13.     if(check = str.length()){
14.         System.out.print(str.charAt(check -= 1) + ", ");
15.     }else{
16.         System.out.print(str.charAt(O) + ", ");
17.     }
18. }

and the invocation:

21. test("four");
22. test("tee");
23. test("to");

What is the result?

A. r, t, t,

B. r, e, o,

C. Compilation fails.

D. An exception is thrown at runtime.

答案:

題目範圍:基本觀念

解析:

這題就是考你眼睛尖不尖啦

在下一開始還以為是題目作者筆誤…不過他大方地藏了三處錯誤

StingString

if(check = str.length())if(check == str.length())

System.out.print(str.charAt(O) + ", "); System.out.print(str.charAt(0) + ", ");

arrow
arrow
    全站熱搜

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