ImageVerifierCode 换一换
格式:DOC , 页数:4 ,大小:42.50KB ,
资源ID:1334751      下载积分:5000 积分
快捷下载
登录下载
邮箱/手机:
温馨提示:
如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝扫码支付 微信扫码支付   
注意:如需开发票,请勿充值!
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【http://www.mydoc123.com/d-1334751.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(【计算机类职业资格】国家二级(JAVA)机试-试卷23及答案解析.doc)为本站会员(cleanass300)主动上传,麦多课文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文库(发送邮件至master@mydoc123.com或直接QQ联系客服),我们立即给予删除!

【计算机类职业资格】国家二级(JAVA)机试-试卷23及答案解析.doc

1、国家二级(JAVA)机试-试卷 23 及答案解析(总分:12.00,做题时间:90 分钟)一、基本操作题(总题数:2,分数:4.00)1.基本操作题()(分数:2.00)_2.本程序的功能是,从键盘输入一个整数,存入一个输入流中,然后输出它的两倍值。请将程序补充完整。注意:请勿修改 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; public class basic public static void main(String args) File dir=new File(“.“); Filter filter=new Filter(“ja

2、va“); System.out.println(“list java files in directory“ +dir); String files=dir.list(filter); /列出目录 dir 下,文件后缀名为 java 的所有文件 for(_;_;i+) File f=new File(dir,filesi); _ (f.isFile() /如果该对象为后缀为 java 的文件,则打印文件名 System.out.println(“file“ +f); else System.out.println(“sub directory“ +f); /如果是目录则打印目录名 class

3、 Filter implements FilenameFilter String extent; Filter(String extent) this.extent=extent; public boolean accept(File dir,String name) return name.endswith(“.“+extent);/返回文件的后缀名 (分数:2.00)_二、简单应用题(总题数:2,分数:4.00)3.简单应用题()(分数:2.00)_4.请完成程序,首先由一个类 simple 实现 Serializable 接口,并有三个成员变量,分别为 int 型、double 型和 S

4、tring 型,可以用 toString 的方法显示这三个成员变量。在 main 方法中创建这个 simple的持久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为TheSerial.data 的文件中,并显示成员变量。最后从文件 TheSerial.data 中读出三个成员变量并显示出来。 注意:请勿修改 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; class TheSerial implements Serializable private int intvalue; private double

5、doublevalue; private String string; The Serial () intvalue=123; doublevalue=12.34; string=“Serialize Test“; public void setDouble(double d) doublevalue=d; public void setInt(int i) intvalue=i; public void setString(String s) string=s; public String to String() return(“int=“+intvalue+“ double=“+doubl

6、evalue+“ string=“+string); public class simple public static void main(String args) The Serial e1=new TheSerial(); TheSerial e2; try e1.setInt(Integer.parseInt(args0); e1.setDouble(Double.parseDouble(args1); e1.setString(args2); catch(Exception e) e1.setString(e.getMessage(); System.out.println(e1);

7、 try FileOutputStream oS=new FileOutputStream(“TheSerial.data“); ObjectOutputStream oOS=new ObjectOutputStream(oS); _; catch(IOException ioException) System.out.println (ioException.getMessage (); try FileInputStream iS=new FileInputStream(“TheSerial.data“); ObjectInputStream oIS=new ObjectInputStre

8、am(iS); _; System.out.println(e2); catch(IOException ioException) System.out.println(ioException.getMessage(); catch(ClassNotFoundException cnfException) System.out.println(cnfException.getMessage(); (分数:2.00)_三、综合应用题(总题数:2,分数:4.00)5.综合应用题()(分数:2.00)_6.本题程序中实现了一个“生产者一消费者问题”。生产者产生一个随机数存入 DataPool 类中,

9、消费者从中取出数据。DataPool 类一次只能存放一个数据。请更正题中带下划线的部分。 注意:不改变程序的结构,不得增行或删行。 class DataPool private int data; private boolean isFull; public DataPool() isFull=false; public synchronized void putData(int d) if(isFull= =true) try this.notify(); catch(InterruptedException e) data=d; isFull=true; System.out.printl

10、n(“生产了一个数据:“+data); this.notify(); public synchronized int getData() if(isFull= =false) try this.wait(); catch(InterruptedException e) isFull=false; System.out.println(“消费了一个数据“+data); this.wait(); return this.data; boolean getIsFull() return isFull; class Producer extends Thread DataPool pool; publ

11、ic Producer(DataPool pool) this.pool=pool; public void run() for(int i=0; i10; i+) int data=(int) (Math.random()*1000); try /用于生产数据 sleep(data); catch(InterruptedException e) pool.putData(data); class Consumer implements Runnable DataPool pool; public Consumer(DataPool pool) this.pool=pool; public v

12、oid run() for(int i=0; i10; i+) int data=pool.getData(); try /用于处理数据 sleep(int) (Math.random()*1000); catch(InterruptedException e) public class advance public static void main(String args) Data Pool pool=new Data Pool(); Producer pro=new Producer(pool); Runnable con=new Consumer(pool); Thread conTh

13、=new Thread(con); pro.start(); conTh.start(); (分数:2.00)_国家二级(JAVA)机试-试卷 23 答案解析(总分:12.00,做题时间:90 分钟)一、基本操作题(总题数:2,分数:4.00)1.基本操作题()(分数:2.00)_解析:2.本程序的功能是,从键盘输入一个整数,存入一个输入流中,然后输出它的两倍值。请将程序补充完整。注意:请勿修改 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; public class basic public static void main(String

14、args) File dir=new File(“.“); Filter filter=new Filter(“java“); System.out.println(“list java files in directory“ +dir); String files=dir.list(filter); /列出目录 dir 下,文件后缀名为 java 的所有文件 for(_;_;i+) File f=new File(dir,filesi); _ (f.isFile() /如果该对象为后缀为 java 的文件,则打印文件名 System.out.println(“file“ +f); else

15、System.out.println(“sub directory“ +f); /如果是目录则打印目录名 class Filter implements FilenameFilter String extent; Filter(String extent) this.extent=extent; public boolean accept(File dir,String name) return name.endswith(“.“+extent);/返回文件的后缀名 (分数:2.00)_正确答案:(正确答案:int i=0 ifiles.length if)解析:解析:本题考查知识点:程序流程

16、控制。这道题虽然是一道读取文件的题,但是考查的却仍然是最基本的循环语句和条件分支语句。第 1 个空定义循环的初始条件。第 2 个空定义循环结束条件,当循环次数大于 files 中包含的文件名的个数时,循环完毕。第 3 个空考查 if-else 语句的使用。二、简单应用题(总题数:2,分数:4.00)3.简单应用题()(分数:2.00)_解析:4.请完成程序,首先由一个类 simple 实现 Serializable 接口,并有三个成员变量,分别为 int 型、double 型和 String 型,可以用 toString 的方法显示这三个成员变量。在 main 方法中创建这个 simple的持

17、久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为TheSerial.data 的文件中,并显示成员变量。最后从文件 TheSerial.data 中读出三个成员变量并显示出来。 注意:请勿修改 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; class TheSerial implements Serializable private int intvalue; private double doublevalue; private String string; The Serial () intvalu

18、e=123; doublevalue=12.34; string=“Serialize Test“; public void setDouble(double d) doublevalue=d; public void setInt(int i) intvalue=i; public void setString(String s) string=s; public String to String() return(“int=“+intvalue+“ double=“+doublevalue+“ string=“+string); public class simple public sta

19、tic void main(String args) The Serial e1=new TheSerial(); TheSerial e2; try e1.setInt(Integer.parseInt(args0); e1.setDouble(Double.parseDouble(args1); e1.setString(args2); catch(Exception e) e1.setString(e.getMessage(); System.out.println(e1); try FileOutputStream oS=new FileOutputStream(“TheSerial.

20、data“); ObjectOutputStream oOS=new ObjectOutputStream(oS); _; catch(IOException ioException) System.out.println (ioException.getMessage (); try FileInputStream iS=new FileInputStream(“TheSerial.data“); ObjectInputStream oIS=new ObjectInputStream(iS); _; System.out.println(e2); catch(IOException ioEx

21、ception) System.out.println(ioException.getMessage(); catch(ClassNotFoundException cnfException) System.out.println(cnfException.getMessage(); (分数:2.00)_正确答案:(正确答案:oOs.writeObject(e1) e2=(TheSerial)oIS.readObject()解析:解析:本题考查知识点:串行化要领和目的、串行化方法、基于文本的应用。解题思路:本题主要考查串行化相关的方法和实现。解题中首先要掌握串行化的基本过程和反串行化的过程。串

22、行化过程首先要创建一个 FileOutputStream,通过该类的实例对文件进行访问,然后创建一个 ObjectOutputStream 对象,通过 writeObject()方法来实现对象的序列化。第 1 个空就是使用 writeObject()实现序列化。反序列化过程中用 FileInputStream 对象建立读取文件的连接,并使用该对象创建一个 ObjectInputSream 实例的 readObject()方法就可以实现对象的反序列化。第 2 个空就是使用 readObject()实现反序列化。三、综合应用题(总题数:2,分数:4.00)5.综合应用题()(分数:2.00)_解析

23、:6.本题程序中实现了一个“生产者一消费者问题”。生产者产生一个随机数存入 DataPool 类中,消费者从中取出数据。DataPool 类一次只能存放一个数据。请更正题中带下划线的部分。 注意:不改变程序的结构,不得增行或删行。 class DataPool private int data; private boolean isFull; public DataPool() isFull=false; public synchronized void putData(int d) if(isFull= =true) try this.notify(); catch(InterruptedE

24、xception e) data=d; isFull=true; System.out.println(“生产了一个数据:“+data); this.notify(); public synchronized int getData() if(isFull= =false) try this.wait(); catch(InterruptedException e) isFull=false; System.out.println(“消费了一个数据“+data); this.wait(); return this.data; boolean getIsFull() return isFull;

25、 class Producer extends Thread DataPool pool; public Producer(DataPool pool) this.pool=pool; public void run() for(int i=0; i10; i+) int data=(int) (Math.random()*1000); try /用于生产数据 sleep(data); catch(InterruptedException e) pool.putData(data); class Consumer implements Runnable DataPool pool; publi

26、c Consumer(DataPool pool) this.pool=pool; public void run() for(int i=0; i10; i+) int data=pool.getData(); try /用于处理数据 sleep(int) (Math.random()*1000); catch(InterruptedException e) public class advance public static void main(String args) Data Pool pool=new Data Pool(); Producer pro=new Producer(po

27、ol); Runnable con=new Consumer(pool); Thread conTh=new Thread(con); pro.start(); conTh.start(); (分数:2.00)_正确答案:(正确答案:this.wait() this.notify() thread.sleep(int)(Math.random()*1000)解析:解析:本题考查知识点:多线程同步与互斥、线程的概念和实现方法。解题思路:Data Pool 是一个用来存放数据的缓冲池,其中可以存放一个血型数据,变量 isFull 作为标志量,标志该缓冲池中是否有数据。Put Data()方法负责向

28、 Data Pool 中存放数据,本方法调用成功,缓冲池中就存入了数据,getData()方法负责从 DataPool 中获得数据,本方法调用成功,缓冲池就为空。Producer 类负责产生随机数据,然后将数据存放到 DataPool 中。Consumer 类负责才能够从 DataPool 中取出数据。Producer 和Consumer 共享同一个 Data Pool 对象。当某个线程进入 synchronized 块后,共享的数据并不一定能满足该线程的需要,这样线程就需要等待其他线程修改共享数据,直到满足需要以后才继续执行,但是当前线程必须释放锁以使得其他线程可以运行。wait()和 no

29、tify()方法是实现线程之间通信的两个方法。wait()用来释放线程拥有的锁,使线程进入等待队列;notify()用来唤醒等待队列中的线程,并把它加入到申请锁的队列。本题中生产者在 DataPool 有数据的情况下需要进入等待消费者取出数据,所以需要调用 wait()方法,因此第 1 个下划线的地方应该改为 this.wait()。消费者线程在取出数据以后,必须通知生产者线程 DataPool 中已经没有数据了,因此第 2 个下划线的地方改为 this.notify()。第 3 个下划线的地方,考查常用的线程类的使用。Runnable 接口的目的是使任何类都可以为线程提供线程体,但它本身并不是线程,所以并不能直接调用 Thread 类的方法,需要改为 thread.sleep。

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1