JAVA Programming Language Homework VI: Threads & I/O ID: Name: 1. When comparing java.io.bufferedwriter to java.io.filewriter, which capability exists as a method in only one of the two? A. Closing the stream B. Flushing the stream C. Writing to the stream D. Marking a location in the stream E. Writing a line separator to the stream E A : 兩者皆有關閉串流的方法 B : 兩者皆有清除串流的方法 C : 兩者皆有寫出串流的方法 D : 兩者皆沒有標記串流位置的方法, 通常是用在讀入串流使用 E : BufferedWriter 有寫出換列 newline() 方法 ; FileWriter 則無 2. Which of the following is true? A. A program will terminate only when all user threads stop running. B. A program will terminate only when all daemon stop running. C. A daemon thread always runs at Thread.MIN_PRIORITY. D. None of the above. A 執行緒分為 : 使用者執行緒 (user) 和背景執行緒 (daemon), 當 JVM 要關閉程式時, 若發覺仍有使用者執行緒, 則程式將不會結束, 必須等待所有使用者執行緒 (user threads) 停止後才能終止程式 補充 : 在呼叫 start() 方法前, 可透過 setdaemon 方法變更其為背景執行緒, 如 : mythread.setdaemon(true); mythread.start( start(); 且其優先權等於 NORM_PRIORITY
3. Given the following Java code: 1. class B implements Runnable{ 2. public void run() {} 3. } 4. class A { 5. public static void main(string[] args) { 6. Thread my1 = new Thread() ; 7. Thread my2 = new Thread("B") ; 8. Thread my3 = new Thread(new B()) ; 9. Thread my4 = new Thread("B", new B()) ; 10. } What is the result? A. A compile-time error is generated at line 6 B. A compile-time error is generated at line 7 C. A compile-time error is generated at line 8 D. A compile-time error is generated at line 9 E. None of the above D Thread 並無該選項之建構子 Thread 建構子如下 : Thread() Thread(Runnable target) Thread(String name) Thread(Runnalbe target, String name) Thread(ThreadGroup group, Runnable target) Thread(ThreadGroup group, String name) Thread(ThreadGroup group, Runnable target, String name) Thread(ThreadGroup group, Runnable target, String name, long stacksize)
4. Given the following Java code: [5 points] 1. class B extends Thread { 2. public String x; 3. B(String in) { 4. x = in; 5. } 6. public void run() { 7. for(int i=1; i<5; i++) { 8. System.out.println(x+ - +i); 9. } 10. } 12. 13. class A { 14. public static void main(string[] args) { 15. B obj1 = new B( o ); 16. B obj2 = new B( x ); 17. obj1.setpriority(1); 18. obj2.setpriority(10); 19. obj1.start(); 20. obj2.start(); 21. } 22. } Which of the following is true? A. This program will go exception when compiling. B. Obj1 runs at Thread.MIN_PRIORITY. C. Obj2 runs at Thread.MIN_PRIORITY. D. The Thread.setDaemon method can change Thread.MIN_PRIORITY. E. None of the above. B Thread.MIN_PRIORITY 代表數值是 1 Thread.NORM_PRIORITY 代表數值是 5 Thread.MAX_PRIORITY 代表數值是 10 Thread.setDaemon 優先權屬於 NORM_PRIORITY
5. Given the following Java code: 1. class A extends Thread { 2. private String i; 3. public void run() { 4. i = A ; 5. } 6. public static void main(string[ ] args) { 7. A a = new A(); 8. a.start(); 9. System.out.print(a.i); 10. } Which of the following are possible results of attempting to compile and run the program? A. prints: A B. prints: 0 C. prints: null D. prints: i E. Compile-time error C 當執行緒物件建立之後, 呼叫 start() 方法之時, 並不會立即執行 run 裡的內容, 所以此題在未更新成員變數 i 的值之前, 便已先執行第 9 行而印出 null 6. Given the following Java code: 1. public B extends Thread { 2. public void run() { 3. System.out.print( A ); 4. } 5. } 6. class A { 7. public static void main (String[] args) { 8. B obj = new B() ; 9. obj.start() ;
10. obj.start() ; 12. } What is the result of attempting to compile and run the program? A. The program compiles and runs fine but prints nothing. B. prints: A C. Compiler error D. An IllegalThreadStateException is thrown at run-time E. None of the above D 當呼叫 start() 方法之後, 不可再呼叫一次 start() 方法, 雖然可以編譯成功, 但會發生執行階段時的例外 7. Given the following Java code: 1. public class Hello implements Runnable { 2. public void run (){ 3. System.out.print ("running"); 4. } 5. public static void main ( String[] args ){ 6. Thread t = new Thread ( new Hello()); 7. t.run (); 8. t.run (); 9. t.start (); 10. } What is the result? A. Compilation fails B. An exception is thrown at runtime C. The code executes and prints "running" D. The code executes and prints "runningrunning" E. The code executes and prints "runningrunningrunning"
E 第 7 列呼叫 run(), 是以一般物件方法呼叫並輸出 "running" 第 8 列與第 7 列相同 第 9 列以執行緒方式啟動 t 的 Thread 中的 run() 並輸出 "running" 8. Chain these constructors to create objects to read from a file named "in" and to write to a file named "out". 1. Reader = [1. place here] [2. place here] "in")); 2. Writer = [3. place here] [4. place here] [5. place here] "out")); Constructors: A. new FileReader ( B. new PrintReader ( C. new BufferedReader ( D. new BufferedWriter ( E. new FileWriter ( F. new PrintWriter ( Which sequence is correct? A. CAFDE B. ACDFE C. CAEDF D. CBFDE E. BCDFE A Reader = new BufferedReader ( new FileReader ("in")); Writer = new PrintWriter ( new BufferedWriter ( new FileWriter ("out")); 備註 : 並沒有 PrintReader 的類別, 相關資訊可洽 : http://java.sun.com/j2se/1.4.2/docs/api/java/io/package /api/java/io/package-summary.html 9. Place the code fragments into position to use a BufferedReader to read in an entire text file. 1. class PrintFile { 2. public static void main (String[] args) { 3. BufferedReader buffreader = null;
4. // more code here to initialize buffreader 5. try { 6. String temp; 7. while( [1. place here] [2. place here] ) { 8. System.out.println(temp); 9. } 10. } catch [3. place here] 11. e.printstacktrace(); 12. } 13. } 14. } Code Fragments: A. (temp = buffreader.readline ()) B. && buffreader.hasnext () C. (temp = buffreader.nextline ()) D. (IOException e) { E.! = null F. (FileNotFoundException e) { Which sequence is correct? A. AED B. AEF C. ABD D. CBF E. CED A while( (temp = buffreader.readline ())!= null ){ System.out.println(temp); } } catch (IOException e) { temp 很明顯的是用來讀取資料的變數 ; 之前只有 BufferedReader, 所以是利用 BufferedReader 的 method 來讀取, 其中可選可選的答案答案只有 readline(), 因為並沒有 nextline() 這個 method 存在 放在 while 中, 需要的是一個邏輯運算式, 例如表示讀不到資料或是結尾時, 由於 readline() 會 return null, 因此利用!= null 作為判斷的依據 readline() 會丟出 IOException(); 而不是 FileNotFoundException
10. Place the Fragments into program, so that the program will get lines from a text file, display them, and then close the resources. 1. import java.io.*; 2. public class ReadFile { 3. public static void main (String[] args) { 4. try { 5. File x1 = new File("MyText.txt"); 6. [1. Place here] x2 = new [2. Place here](x1); 7. [3. Place here] x4 = new [4. Place here](x2); 8. String x3 = null; 9. while(( x3 = x4.[5. place here]())!= null ){ 10. System.out.println(x3); 12. x4.close(); 13. } 14. catch (Exception ex) { 15. ex.printstacktrace (); 16. } 17. } 18. } Code Fragments: A. BufferedReader B. StreamReader C. FileReader D. readline E. readln F. read G. closefile F. close Try to fill them: 1. 2. 3. 4. 5. 1. C 2. C 3. A 4. A 5. D - 本意是讀取文件檔 MyText.txt 的資料, 並將之呈現出來 - 其中先利用 FileReader 讀取在將之放入 BufferedReader 中, 以減低不斷 讀取硬碟的負擔 - BufferedReader 並無提供 readln 方法 - read() 用來讀取單一字元, 回傳型態為 int, 而 readline 則一次一次讀取一 行且回傳型態為 string