Microsoft Word - JAVA Programming Language Homework VI_ans.doc

Similar documents
Chapter 9: Objects and Classes

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

3.1 num = 3 ch = 'C' 2

1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6:

chp6.ppt

untitled

JavaIO.PDF

EJB-Programming-4-cn.doc

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

2009年3月全国计算机等级考试二级Java语言程序设计笔试试题

雲端 Cloud Computing 技術指南 運算 應用 平台與架構 10/04/15 11:55:46 INFO 10/04/15 11:55:53 INFO 10/04/15 11:55:56 INFO 10/04/15 11:56:05 INFO 10/04/15 11:56:07 INFO

Learning Java


Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

epub83-1

untitled

Chapter 9: Objects and Classes

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

用手機直接傳值不透過網頁連接, 來當作搖控器控制家電 ( 電視遙控器 ) 按下按鍵發送同時會回傳值來確定是否有送出 問題 :1. 應該是使用了太多 thread 導致在傳值上有問題 2. 一次按很多次按鈕沒辦法即時反應

《大话设计模式》第一章

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

D C 93 2

Java Access 5-1 Server Client Client Server Server Client 5-2 DataInputStream Class java.io.datainptstream (extends) FilterInputStream InputStream Obj

CC213

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

内 容 简 介 本 书 是 一 本 关 于 语 言 程 序 设 计 的 教 材, 涵 盖 了 语 言 的 基 本 语 法 和 编 程 技 术, 其 中 包 含 了 作 者 对 语 言 多 年 开 发 经 验 的 总 结, 目 的 是 让 初 学 的 读 者 感 受 到 语 言 的 魅 力, 并 掌

untitled

可 愛 的 動 物 小 五 雷 雅 理 第 一 次 小 六 甲 黃 駿 朗 今 年 暑 假 發 生 了 一 件 令 人 非 常 難 忘 的 事 情, 我 第 一 次 參 加 宿 營, 離 開 父 母, 自 己 照 顧 自 己, 出 發 前, 我 的 心 情 十 分 緊 張 當 到 達 目 的 地 後

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

Microsoft Word - template.doc

The Embedded computing platform

Swing-02.pdf

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

高中英文科教師甄試心得

(6) 要 求 付 款 管 理 员 从 预 订 表 中 查 询 距 预 订 的 会 议 时 间 两 周 内 的 预 定, 根 据 客 户 记 录 给 满 足 条 件 的 客 户 发 送 支 付 余 款 要 求 (7) 支 付 余 款 管 理 员 收 到 客 户 余 款 支 付 的 通 知 后, 检

SDK 概要 使用 Maven 的用户可以从 Maven 库中搜索 "odps-sdk" 获取不同版本的 Java SDK: 包名 odps-sdk-core odps-sdk-commons odps-sdk-udf odps-sdk-mapred odps-sdk-graph 描述 ODPS 基

Microsoft Word - 11.doc

Microsoft Word - 01.DOC

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

EJB-Programming-3.PDF

FileMaker 16 ODBC 和 JDBC 指南

while ((ch = fr.read())!= -1) { System.out.print((char) ch); fr.close(); 例 3: 用 BufferedReader 读 TXT 文件 public class FileReaderDemo3 { public static v

2009年9月全国计算机等级考试二级Java真题及答案

目 录 实 验 一 : 熟 悉 Java 开 发 环 境... 1 实 验 二 : 类 与 面 向 对 象... 7 实 验 三 : 继 承 和 多 态 实 验 四 : 异 常 处 理 实 验 五 : 流 文 件 及 基 于 文 本 的 应 用... 23

2 2 3 DLight CPU I/O DLight Oracle Solaris (DTrace) C/C++ Solaris DLight DTrace DLight DLight DLight C C++ Fortran CPU I/O DLight AM

Microsoft Word - ch04三校.doc

附录J:Eclipse教程

Microsoft Word - JAVA Programming Language Homework I ans

Microsoft PowerPoint - 14Threads.ppt

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

Microsoft PowerPoint - L17_Inheritance_v4.pptx

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

投影片 1

Microsoft Word - 投影片ch14

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

x MapReduce A Italic URL Constant width Constant width bold Constant width italic 這個圖示代表提示或建議 這個圖示代表一般註解

untitled

Java

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc

Microsoft Word - 11月電子報1130.doc

全国计算机技术与软件专业技术资格(水平)考试

任務二 : 產生 20 個有炸彈的磚塊, 放在隨機的位置編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class

提纲 1 2 OS Examples for 3

INTRODUCTION TO COM.DOC

國家圖書館典藏電子全文

Java 1 Java String Date

D getinitparameternames() 9 下 列 选 项 中, 属 于 Servlet API 中 提 供 的 request 对 象 的 包 装 类 的 是 ( ) A HttpServletRequestWrapper B HttpServletRequest C HttpServ

使用MapReduce读取XML文件

untitled

Windows XP

JBuilder Weblogic

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数

untitled

计 算 机 系 统 应 用 年 第 25 卷 第 4 期 线 程 的 复 用 [2,3]. 通 常 情 况 下, 服 务 器 端 程 序 在 启 动 时 创 建 若 干 数 量 的 线 程 对 象 并 缓 存 起 来, 此 时 它 们 处 于

Microsoft Word - Learn Objective-C.doc

内 容 提 要 将 JAVA 开 发 环 境 迁 移 到 Linux 系 统 上 是 现 在 很 多 公 司 的 现 实 想 法, 而 在 Linux 上 配 置 JAVA 开 发 环 境 是 步 入 Linux 下 JAVA 程 序 开 发 的 第 一 步, 本 文 图 文 并 茂 地 全 程 指

untitled

javaexample-02.pdf

4. 每 组 学 生 将 写 有 习 语 和 含 义 的 两 组 卡 片 分 别 洗 牌, 将 顺 序 打 乱, 然 后 将 两 组 卡 片 反 面 朝 上 置 于 课 桌 上 5. 学 生 依 次 从 两 组 卡 片 中 各 抽 取 一 张, 展 示 给 小 组 成 员, 并 大 声 朗 读 卡

概述

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

Untitiled

Microsoft Word - ChineseSATII .doc

基于ECO的UML模型驱动的数据库应用开发1.doc

AL-M200 Series

2 Java 语 言 程 序 设 计 教 程 简 单 性 Java 语 言 的 语 法 与 C 语 言 和 C++ 语 言 很 接 近, 使 得 大 多 数 程 序 员 很 容 易 学 习 和 使 用 Java 另 一 方 面,Java 丢 弃 了 C++ 中 很 少 使 用 的 很 难

TX-NR3030_BAS_Cs_ indd

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

2-7.FIT)

(Microsoft Word - 10\246~\253\327\262\304\244@\264\301\256\325\260T_Version4)

mvc

untitled

Android Service

马 大 华 人 文 学 与 文 化 学 刊 Journal of Chinese Literature and Culture 6 前 言 顾 城 曾 在 接 受 德 国 汉 学 家 顾 彬 及 张 穗 子 专 访 中, 将 其 诗 歌 创 作 分 为 四 个 时 期, 即 自 然 阶 段 文 化

PowerPoint Presentation

科学计算的语言-FORTRAN95

國立中山大學學位典藏

如何挑选适合企业沟通的“聊天”软件?

Microsoft Word - TIP006SCH Uni-edit Writing Tip - Presentperfecttenseandpasttenseinyourintroduction readytopublish

Transcription:

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