"SCJP 1.6版考題 001" 修訂間的差異
出自 陳富國維基館
行 2: | 行 2: | ||
</span></span> | </span></span> | ||
− | <br>Given: | + | <br>Given: |
− | + | ||
− | + | 1. public class Threads4{ | |
− | + | 2. public static void main(String[] args){ | |
− | + | 3. new Threads4.go(); | |
− | 1. public class Threads4{ | + | 4. } |
− | + | 5. public void go(){ | |
− | 2. public static void main(String[] args){ | + | 6. Runnable r = new Runnable(){ |
− | + | 7. public void run(){ | |
− | 3. new Threads4.go(); | + | 8. System.out.print("foo"); |
− | + | 9. } | |
− | 4. } | + | 10. }; |
− | + | 11. Thread t = new Thread(r); | |
− | 5. public void go(){ | + | 12. t.start(); |
− | + | 13. t.start(); | |
− | 6. Runnable r = new Runnable(){ | + | 14. } |
− | + | 15. } | |
− | 7. public void run(){ | + | <br> |
− | + | What is the result? | |
− | 8. System.out.print("foo"); | + | A. Compilation fails. |
− | + | B. An exception is thrown at runtime. | |
− | 9. } | + | C. The code executes normally and prints "foo"; |
− | + | D. The code executes normally, but nothing is printed. | |
− | 10. }; | ||
− | |||
− | 11. Thread t = new Thread(r); | ||
− | |||
− | 12. t.start(); | ||
− | |||
− | 13. t.start(); | ||
− | |||
− | 14. } | ||
− | |||
− | 15. } | ||
− | |||
− | <br> | ||
− | |||
− | What is the result? | ||
− | |||
− | A. Compilation fails. | ||
− | |||
− | B. An exception is thrown at runtime. | ||
− | |||
− | C. The code executes normally and prints "foo"; | ||
− | |||
− | D. The code executes normally, but nothing is printed. | ||
− | |||
<div class="toccolours mw-collapsible mw-collapsed"> | <div class="toccolours mw-collapsible mw-collapsed"> |
於 2013年3月30日 (六) 09:13 的最新修訂
Given: 1. public class Threads4{ 2. public static void main(String[] args){ 3. new Threads4.go(); 4. } 5. public void go(){ 6. Runnable r = new Runnable(){ 7. public void run(){ 8. System.out.print("foo"); 9. } 10. }; 11. Thread t = new Thread(r); 12. t.start(); 13. t.start(); 14. } 15. }
What is the result? A. Compilation fails. B. An exception is thrown at runtime. C. The code executes normally and prints "foo"; D. The code executes normally, but nothing is printed.
解答
Ans: E
解說: 一個執行緒只能啟動一次 (t.start()只能下一次),執行緒是物件,物件相關的動作發生問題是在執行時期產生例外,而不是編譯的錯誤。
|