1、二级 JAVA 机试 27 及答案解析(总分:100.00,做题时间:90 分钟)一、B1基本操作题/B(总题数:1,分数:30.00)1.下面程序的功能是从键盘读取一行文本并将该文本存储到文件中。当用户运行该程序时,需要输入一行文本并按下回车键。然后在 DOS 系统提示符处输入 type file.txt 并按下 Enter 键,这时屏幕会显示type file.txt 的内容。通过这个命令可以验证该程序的功能。请在每条横线处填写一条语句,使程序的功能完整。 注意:请勿改动 main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 源程序文件代码清单如下: import java.
2、io.*; public class DoFile public static void main(String args) byte buffername=_80; try System.out.println(“/nEnter a line to be saved to disk:“); int bytesbleck= _; FileOutputStream f=new FileOutputStream(“file.txt“); _; catch(Exception e) e.printStackTrace(); (分数:30.00)_二、B2简单应用题/B(总题数:1,分数:40.00)
3、2.请完成下列 Java 程序。程序的功能是显示用户在命令行方式下指定的任意驱动器文件夹的内容。 提示:public string()list();/将文件夹中所有文件名保存在字符数组中返回。 注意:请勿改动 main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。 源程序文件代码清单如下: import java.io.*; public class FindDirectories public static void main(String args) if(args.length=0) args=new String()“; try _; String fileName=pa
4、thName.list();for(int i=0;ifileName.length;i+) File f=new File(pathName.getPath(),fileNamei); if(_) System.out.println(f.getCanonicalPath(); main(new String f.getPath() ); catch(IOException e) e.printStackTrace(); (分数:40.00)_三、B3综合应用题/B(总题数:1,分数:30.00)3.下面是一个 Applet 程序,其功能是有两个按钮,分别为 First 和 Second,以
5、及一个 Label 构件。要求单击 First 时能在 Label 中显示出“Command:First“,而单击 Second 时能显示出“Command:Second“,要求只能重载一次 actionPerformed()方法,请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 源程序文件代码清单如下: import java.awt.*; import java.awt.event.*; import java.applet.*; /* applet code=ex04_3.class width=800 height=400 /ap
6、plet */ Public class ex04_3 extends Applet implements ActionListener private String str=“ok“; private Label l; private Button btn; public void init() setLayout(null); l=new Label(str); l.reshape(10,10,100,30); add(l); btn=new Button(“First“); btn.reshape(10,50,60,20); Ul.addActionListene(this);/U ad
7、d(btn); btn=new Button(“Second“); btn.reshape(10,100,60,20); Ubtn.setActionCommand(“First“);/U btn.addActionListener(this); add(btn); public void actionPerformed(ActionEvent ae) str=“Command:“+ae.getActionCommand(); Ubtn.setText(str);/U Bex04_3.html/B HTML HEAD TITLEex04_3/TITLE /HEAD BODY applet co
8、de=“ex2_3.class“ width=800 height=400 /applet /BODY /HTML(分数:30.00)_二级 JAVA 机试 27 答案解析(总分:100.00,做题时间:90 分钟)一、B1基本操作题/B(总题数:1,分数:30.00)1.下面程序的功能是从键盘读取一行文本并将该文本存储到文件中。当用户运行该程序时,需要输入一行文本并按下回车键。然后在 DOS 系统提示符处输入 type file.txt 并按下 Enter 键,这时屏幕会显示type file.txt 的内容。通过这个命令可以验证该程序的功能。请在每条横线处填写一条语句,使程序的功能完整。
9、注意:请勿改动 main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 源程序文件代码清单如下: import java.io.*; public class DoFile public static void main(String args) byte buffername=_80; try System.out.println(“/nEnter a line to be saved to disk:“); int bytesbleck= _; FileOutputStream f=new FileOutputStream(“file.txt“); _; catch(Excep
10、tion e) e.printStackTrace(); (分数:30.00)_正确答案:()解析:new byte System.in.read(buffername) f.write(buffername,0,bytesblock) 解析 本题主要考查基本数据类型、对文件的操作知识。解答本题的关键是熟练掌握基本数据类型、对文件的操作知识。在本题中,byte buffemame=byte100;语句的功能是定义含 100 个整型元素的数组 buffername, System.in.read(buffername);语句的功能是获得标准控制台的输入数据,f.write(buffemame,0
11、, bytesblock)语句的功能是将从标准控制台获得的输入数据写入文件输出流中。二、B2简单应用题/B(总题数:1,分数:40.00)2.请完成下列 Java 程序。程序的功能是显示用户在命令行方式下指定的任意驱动器文件夹的内容。 提示:public string()list();/将文件夹中所有文件名保存在字符数组中返回。 注意:请勿改动 main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。 源程序文件代码清单如下: import java.io.*; public class FindDirectories public static void main(String
12、args) if(args.length=0) args=new String()“; try _; String fileName=pathName.list();for(int i=0;ifileName.length;i+) File f=new File(pathName.getPath(),fileNamei); if(_) System.out.println(f.getCanonicalPath(); main(new String f.getPath() ); catch(IOException e) e.printStackTrace(); (分数:40.00)_正确答案:(
13、)解析:File pathName=new File(args0) f.isDirectory() 解析 本题主要考查文件操作的基本知识。解答本题的关键是熟练掌握对文件操作的基本知识。其中,File pathName=new File(args0)语句的功能是生成 File 类对象 pathName; if(f.isDirectory()语句的功能是判断生成的 f 对象是否为文件夹。三、B3综合应用题/B(总题数:1,分数:30.00)3.下面是一个 Applet 程序,其功能是有两个按钮,分别为 First 和 Second,以及一个 Label 构件。要求单击 First 时能在 Labe
14、l 中显示出“Command:First“,而单击 Second 时能显示出“Command:Second“,要求只能重载一次 actionPerformed()方法,请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 源程序文件代码清单如下: import java.awt.*; import java.awt.event.*; import java.applet.*; /* applet code=ex04_3.class width=800 height=400 /applet */ Public class ex04_3 exte
15、nds Applet implements ActionListener private String str=“ok“; private Label l; private Button btn; public void init() setLayout(null); l=new Label(str); l.reshape(10,10,100,30); add(l); btn=new Button(“First“); btn.reshape(10,50,60,20); Ul.addActionListene(this);/U add(btn); btn=new Button(“Second“)
16、; btn.reshape(10,100,60,20); Ubtn.setActionCommand(“First“);/U btn.addActionListener(this); add(btn); public void actionPerformed(ActionEvent ae) str=“Command:“+ae.getActionCommand(); Ubtn.setText(str);/U Bex04_3.html/B HTML HEAD TITLEex04_3/TITLE /HEAD BODY applet code=“ex2_3.class“ width=800 heigh
17、t=400 /applet /BODY /HTML(分数:30.00)_正确答案:()解析:btn.addActionListener(this) btn.setActionCommand(“second“) l.setText(str) 解析 本题主要考查 Java 语言中高级事件 AcfionEvent 和 AWT 基本构件 Label 的常用方法的使用。解题关键是熟练掌握动作事件 ActionEvent 和 Label 构件的常用方法。在本题中,第 1 处,明确注册的事件监听器是监听按钮的,而不是 Label;第 2 处,调用 ActionEvent 的 setActionCommand()方法改变了ActionCommand,使按下 Second 按钮时显示 “Command:second“,而不是“Command:First“;第 3 处,调用Label 的 setText()方法,而不是 Button 的方法。程序运行结果如下图所示。