浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编
版权所有 不得转载 违者必究
参考答案
一、判断题
1-10 对,错,对,错,错,对,错,对,对,对 11-20 对,错,错,错,对,错,对,对,对,对 21-30 对,对,对,错,错,对,错,对,对,对 31-40 对,对,对,对,错,对,错,对,对,对 41-50 对,对,错,对,错,错,对,错,错,错
二、填空题
1、javac,java,jar,appletview 2、0.0f,false,基本,引用 3、6,248 4、false,3 5、MyPrg.java
6、Interger,parseInt() 7、2
8、抽象(或abstract),final 9、“abc”
10、引用(或复合),arraycopy 11、package MyPackage,第一行 12、implements 13、new,protected 14、继承,接口
15、对象,interface
16、abstract, 提供了一个基本类,实现代码或算法的重用
17、Object,import java.util.* ; 18、Error和Exception
19、finally,throw,throws
20、InputStream,OutputStream,字节,字符
21、IOException,ArrayIndexOutOfBoundsException
22、new FileInputStream(“c:\\\\test.txt”),readLine()
23、RandomAcessFile raf=new RandomAcessFile(“c:\\\\test.txt”,“rw”);
i= raf.readInt(); 24、GridLayout、CardLayout
25、轻量级,getContentPane(),setLayout 26、JFrame,JWindow,JDialog
27、Applet,MyApplet.java 28、init(),destroy() 29、add,GUI
30、Thread,Runnable 31、start,run
32、synchronized,
从全局考虑获得锁的顺序,并且在整个程序中都遵守这个顺序;释放锁时,要按加锁的反序释放。
第 1 页 共 32 页
浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编
版权所有 不得转载 违者必究
33、sleep,setPriority()
34、Runnable,public void run(),new Thread(rt),myThread.start() 35、TCP,UDP
36、getPort(),getLength() 37、ServerSocket,Socket 38、ServerSocket,Socket 39、accept(),receive() 40、accept(),receive()
三、选择题
1-10 BCCBA DCCAA 11-20 BABCD BABBC 21-30 CDBCB DADBC 31-40 CABBD ACCAA 41-50 DBADB DCABA 51-60 DADDD ADDCD 61-70 DABCB ACCBB 71-80 CDCBD ABDDC
四、程序阅读题
1、输出结果如下: Second season 3th season
2、输出结果如下: i=1 j=2
3、输出结果如下: 23
4、输出结果如下: Hi!
I am Tom
How do you do?
5、第8行编译出错,原因是第8行调用11行构造方法时,int参数不能与short类型匹配
6、输出结果如下: 6
7、第6行编译出错,原因是无法调用另一个类中private修饰的方法
8、输出结果如下:
第 2 页 共 32 页
浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编
版权所有 不得转载 违者必究
Hello Yava
9、输出结果如下:
捕获ArithmeticException异常
java.lang.ArithmeticException: 没有13月份!
10、输出结果如下:
userException: 数组可能越界! 退出返回!
11、输出结果如下: 捕获了一个零除异常 12、
1) container.put( fancyButton ); 改为container.add( fancyButton ); 2) container.put( plainButton );改为container.add( plainButton ); 3) fancyButton.addItemListener( handler );改为
fancyButton.addActionListener( handler );
4) plainButton.addItemListener( handler );改为
plainButton.addActionListener( handler );
5) itemStateChanged改为actionPerformed
13、输出结果如下: 张三的工资为1000元。 异常:工资不能为负!
14、输出结果如下:
size in Inner.doStuff(): 11 size of the Inner class: 3 size of the Outer class: 2
15、输出结果如下: 15 11 10
16、有两种修改方法:
(1)如果采用程序中的implements Runnable的方法,则修改的地方有以下3处,: ①第2行String str[]=new String[10];改为 static String str[]=new String[10];
②第11行的sleep(1000);改为Thread.sleep(1000); ③第20行的代码修改为以下两行:
第 3 页 共 32 页
浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编
版权所有 不得转载 违者必究
Thread tt=new Thread(t); //tt的变量名可取符合java语言规范的任何标识符 tt.start();
(2如果采用extends实现该线程程序,则修改的地方有以下3处: ①第1行的implements Runnable修改为extends Thread ②第2行String str[]=new String[10];改为 static String str[]=new String[10]; ③第20行的t.run(); 修改为t.start();
17、输出结果如下: java iavaC Yava 18、
18. System.out.println(str1= =str2); 输出true 19. System.out.println(str2= =str3); 输出true 20. System.out.println(str2= =str4); 输出false 21. System.out.println(str4= =str5); 编译出错 22. System.out.println(str3.equals(str4)); 输出true 23. System.out.println(str4.equals(str5)); 输出false 24. System.out.println (stringReplace (str1)); 输出iavaC 25. System.out.println(bufferReplace (str5)); 输出iavaC 26. System.out.println (str1); 输出java 27. System.out.println(str5); 输出iavaC
19、有两种修改方法:
(1)如果采用程序中的implements Runnable的方法,则修改的地方有以下4处,每改对一处给2分,总共8分:
①第3~5行写入构造方法,并且将第3行的str.length()改为str.length,修改后的代码如下:
public Test() {
for (int i = 0; i < str.length; i++) { str[i]=i+\ } }
②第8行的str.length()改为str.length
③第9行的sleep(1000);改为Thread.sleep(1000); ④第18行的代码修改为以下两行:
Thread tt=new Thread(t); //tt的变量名可取符合java语言规范的任何标识符 tt.start();
(2如果采用extends实现该线程程序,则修改的地方有以下4处,每改对一处给2分,总共8分:
第 4 页 共 32 页
浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编
版权所有 不得转载 违者必究
①第一行的implements Runnable修改为extends Thread
②第3~5行写入构造方法,并且将第3行的str.length()改为str.length,修改后的代码如下:
public Test() {
for (int i = 0; i < str.length; i++) { str[i]=i+\ } }
③第8行的str.length()改为str.length ④ 第18行的t.run(); 修改为t.start(); 20、
(1)小应用程序在加载阶段,在屏幕上打印结果是: Applet初始化? Applet启动...
(2)关闭这个小应用程序时,在屏幕上打印结果是: Applet停止运行... 准备卸载...
21、输出结果如下: My Func
22、输出结果如下: eab
23、输出结果如下: 2
24、输出结果如下: The index is 10 The index is 20 The index is 30
25、输出结果如下: 10020
26、输出结果如下: x=2 st.y=1 x=2 st.y=1
27、输出结果如下: two
第 5 页 共 32 页