第十六題 

Which two code fragments will execute the method doStuff() in separate thread?
(choose two)

A.
new Thread ( ) { 
public void run ( ) { doStuff ( ); } 
}; 

B.
new Thread ( ) { 
public void run ( ) { doStuff ( ); } 
}; 

C.
new Thread ( ) { 
public void run ( ) { doStuff ( ); } 
}; run ( ); 

D.
new Thread ( ) { 
public void run ( ) { doStuff ( ); } 
}; start ( ); 

E.
new Thread (new Runable ( ) { 
public void run ( ) { doStuff ( ); } 
}; run ( ) ; 

F.
new Thread (new Runnable ( ) { 
public void run ( ) { doStuff ( ); } 
}), start ( ); 

答案:DF

執行結果:無 

題目範圍:Thread 執行緒、InnerClass

解析:

這題除了執行緒的概念之外,還使用了InnerClass 的概念
在宣告的同時覆寫類別,用到的是匿名InnerClass

請參閱:InnerClass 的解析

這題有兩處要注意,第一個是doStuff () 方法要被呼叫,第二個是要在另外的執行緒下執行
A與B雖然寫出了內容,最後卻沒有觸發運作的start() 所以不對
C與E最後用的是run() 方法直接呼叫函式,不過這樣雖然會執行,但不會用到執行緒的功能,所以不對

 


 

第十七題 

Which three will compile and run without exception? (choose three) 

A. private synchronized object o; 

B. void go ( ) { 
synchronized ( ) { /* ocde here */ } 

C. public synchronized void go ( ) { /* code here */ } 

D. private synchronized (this) void go ( ) { /* code here */ } 

E. void go ( ) { 
synchronized (object.class) { /* code here */ } 

F. void go ( ) { 
synchronized (o) { /* code here */ } }

答案:CEF

執行結果:無 

題目範圍:Thread 執行緒

解析:

這題考的是synchronized 關鍵字的用法
synchronized
只能用在保護方法、一個程式的區域(block) 而已
保護方法時,會保護所有方法中用到的變數/物件
保護區域時,只會保護synchronized( ) 中提到的變數/物件

 


 

第十八題

Given the following:

public class Computation extends Thread {

   
private int num;
   
private Boolean isComplete;
   
private int result;

   
public Computation(int num) {
       
this.num = num;
    }
   
public synchronized void run() {
        result = num * 2;
        isComplete =
true;
        notify();
    }
   
public synchronized int getResult() {
       
while (!isComplete) {
           
try {
                wait();
            }
catch (InterruptedException e) {
            }
        }
       
return result;
    }
   
public static void main(String[] args) {
        Computation[] computations =
new Computation[4];
       
for (int i = 0; i < computations.length; i++) {
            computations[i] =
new Computation(i);
            computations[i].start();
        }
       
for (Computation c : computations)
            System.out.print(c.getResult() + "");
    }
}

What can be a result?

A. The code will deadlock
B. The code may run with no output
C. An exception is thrown at runtime
D. The code may run with output "0 6"
E. The code may run with output "2 0 6 4"
F. The code may run with output "0 2 4 6"

答案:

執行結果:答案中

題目範圍:Thread 執行緒

解析:

這題目有點長,不過只要了解執行續的運作,慢慢地分析題目,答案很容易就出來了
答案的選項也很仁慈,沒有陷阱
關鍵點就在於,start() 的動作並不會馬上被運行,而是進入排隊
這題中,一直要等到getResult()方法裡面的wait()被叫到,run()方法才會開始動作

請參閱:多工執行緒

 


 

第十九題

Given the following:

public class Test {
    public static void main(String[] args) throws Exception {
        Thread.sleep(3000);
        System.out.println("sleep");
    }
}

Which statement is true?

A. Compilation fails
B. An exception is thrown at runtime
C. The code executes normally and prints "sleep"
D. The code executes normally, but nothing is printed.

答案:

執行結果:答案中

題目範圍:Thread 執行緒

解析:

簡單得讓人以為有陷阱的題目
要注意的地方是sleep() 方法會擲出InterruptedException wait() 方法一樣
要以try/catch包覆,或是方法擲出InterruptedException

這題是用了在方法擲出Exception 的用法,所以一切正常執行
會在執行3秒後顯示"sleep"

 


 

第二十題

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

執行結果:無

題目範圍:物件觀念

解析:

這題除了考觀念,英文也要看得懂才行
has-a
,含有的關係,例如:大學has a系所
is–a
,屬於的關係,例如:大學is a學校
繼承就是屬於(is–a) 的關係,這很容易理解
Interfaces
的使用與否,跟含有(has-a) 沒有必然關係,C選項錯了
舉一個簡單的含有(has-a) 例子:

class A{ }
class B{
    A variable;
}
這樣一來,B 就含有A 同時,你建立了A 的實體物件
所以D選項正確,在產生含有
(has-a) 關係時,裡面的實體物件可以被使用

 

arrow
arrow
    文章標籤
    JAVA SCJP 考試
    全站熱搜

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