Java习题四.doc_第1页
Java习题四.doc_第2页
Java习题四.doc_第3页
Java习题四.doc_第4页
Java习题四.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 运行下列程序,会产生什么结果?public class X extends Thread implements Runable public void run() System.out.println( this is run(); public static void main(String args) Thread t=newThread(new X(); t.start(); 第一行会产生编译错误 第六行会产生编译错误 第六行会产生运行错误 程序会运行和启动2. 下列哪个方法可用于创建一个可运行的类? public class X implements Runnable public void run() . public class X implements Thread public void run() . public class X implements Thread public int run() . public class X implements Runable protected void run() . public class X extends Thread public void run() . 3. 下列哪些情况可以终止当前线程的运行? 抛出一个例外时 当该线程调用sleep()方法时 当创建一个新线程时 当一个优先级高的线程进入就绪状态时4下列说法正确的是: java语言里的线程是没有优先级的 String类在java.lang包中 java语言支持类的序列化 能序列化的类必须实现java.io.Serializable接口5有关线程的哪些叙述是对的? 一旦一个线程被创建,它就立即开始运行。 使用start()方法可以使一个线程成为可运行的,但是它不一定立即开始运行。 当一个线程因为抢先机制而停止运行,它被放在可运行队列的前面。 一个线程可能因为不同的原因停止(cease)并进入就绪状态。6方法resume()负责恢复哪些线程的执行 通过调用stop()方法而停止的线程 通过调用sleep()方法而停止运行的线程 通过调用wait()方法而停止运行的线程 通过调用suspend()方法而停止运行的线程7以下哪个不是线程的组成部分 处理器 代码 数据 显示器8关于Runnable接口,正确的说法是 实现了Runnable接口就可以用start方法启动 Runable接口提供了通过线程执行程序的最基本的接口 Thread类实现了Runnable接口 Runnable只定义了一个run方法9下面说法正确的是 Java中线程是抢占式的 Java中线程是分时的 Java中的线程可以共享数据 Java中的线程可以共享代码10下面属于Java线程同步方法的方法有 join run wait destroy11下面程序的运行结果是什么?class Test implements Runnable DotThread t; public static void main(String args) Test d=new Test(); d.t=new DotThread(); public void init() t.start(); t=new DashThread().start(); class DotThread extends Thread public void run() for(int index=0;index100;index+) System.out.println(“.”); class DashThread extends Thread public void run() for(int index=0;index100;index+) System.out.println(“-”); nothing 100 dots(.) 200 dots(.) 100 dashes(-) 100 dots(.) and 100 dashes(-)12如果一个线程调用了wait()方法,下列哪些方法可以使这个线程继续运行? join() resume() notify() notifyAll() high priority thread is ready13下列那个方法是用来定义具体的线程执行功能的? start() init() run() main() synchronized()14下列哪些方法可以用来定义一个新的线程类? implement the Runnable interface add a run() method in the class create an instance of Thread extend the Thread class15一个方法在声明为同步方法时,只需要在方法名前面加上什么修饰符? synchronized abstract final static public16下列哪种情况会导致当前线程被停止? 中断异常 线程调用了sleep()方法 创建一个新的线程 有更高优先级的线程准备运行 调用输入流的read()方法17给出程序class Test public static void main(String args) Thread t=new Thread(new RunHandler(); t.start(); 下列哪些说法可以使得上面程序得以运行? RunHandler类必须实现java.lang.Runnable接口 RunHandler类必须继承Thread类 RunHandler类必须提供一个run()方法,并且该方法被声明为public,返回值为viod RunHandler类必须提供一个init()方法18下面程序在编译和运行时,将发生什么?class Test implements Runnable int I=0;public int run() while(true) i+; System.out.println(“i=”+i); return 1; 程序能够编译通过,run方法将打印出增加的i的值 程序能够编译通过,并通过调用start()方法打印出增加的i的值 程序在编译时将产生错误 编译将产生错误,因为while不能接收一个为true的参数19下面程序编译和运行时,将发生什么?public class Test extends Thread public static void main(String args) Test b=new Test(); b.run(); public void start() for(int i=0;i10;i+) System.out.println(“Value of i=”+i); 一个编译时的错误表明没有为这个线程类定义run方法 一个运行时的错误表名没有为这个线程类定义run方法 编译通过并且在运行时输入值09 编译通过但是在运行时没有任何输出20下面说法哪一个可以停止线程的运行? 程序通过调用System.exit(0)停止 有较高优先级的线程出现 调用了stop()方法 调用了Thread类的halt方法21下面哪些是Thread类的方法? yield() sleep(long msec) go() stop()22假设有20个具有相同优先级的线程在等待池中,如果能够唤醒等待池中的第15个线程? 调用resume()方法 调用interrupt()方法 调用call()方法 通过在线程实例对象中调用notify(15)方法 以上说法都不对23下面说法哪些是正确的? toString()方法是在Object类中定义的。 toString()方法是在Class类中定义的。 wait(),notify(),notifyAll()方法是在Object类中定义的,用来线程之间的通信。 toString()方法提供了对象的字符串表示24下面的程序代码执行情况是什么?public class Test implements Runnable public void run(String s) System.out.println(“Executing Runnable Interface Thread”); public static void main(String args) Test rt=new Test(); Thread t=new Thread(rt); t.start(); 编译错误 运行错误 编译通过,在屏幕上打印出“Executing Runnable Interface Thread” 编译通过,但不在屏幕上打印出任何信息25下面说法哪些是正确的? Thread类中定义的start()方法是用来启动线程的。 线程执行体run()方法运行结束,意味着线程结束。 被撤销的线程能被重新启动。 Thread类中的stop()方法可以强制当前线程停止执行。26下面程序在编译和运行时将发生什么?1: public class Test extends Thread2: 3: public void run()4: 5: System.out.println(“Befor start method”);6: this.stop();7: System.out.println(“After stop method”);8: 9: public static void main(String args)10: 11: Test a=new Test();12: a.start();13: 14: 选择正确地答案 第7行编译错误。 第7行运行异常。 打印出“Befor start method”和“After stop method”。 只打印出“Befor start method”。 27下列哪些方法可以停止当前线程的执行 sheep(); stop(); yield(); wait(); notify(); notifyAll(); synchronized();28下面程序代码编译运行的结果是什么?public class Test extends Thread public void run() System.out.println(“In run”); suspend(); resume(); System.out.println(“Leaving run”);public static void main(String args)(new Test().start();选择最合适的答案 在main方法中编译失败 在run方法中编译失败 在run方法中将产生一个警告 字符串“In run”将被输出 两个字符串都将被输出 什么也不会发生29下面哪写方法不能直接导致线程停止 notify() wait() notifyAll() sleep()30下面的说法哪些是正确的? 所有被创建的线程同时执行。 如果你想,那么你可以立即停止一个线程。 只可以通过继承Thread类来启动一个线程。 当main()主线程退出了,JVM就将退出,即使有前台的线程仍在运行。31下面哪个不是InputStream类中的方法? int read(byte) void flush() void close() int available()32哪个不是FilterInputStream的子类? DataInputStream BufferedInputStream PushbackInputStream FileInputStream33哪些类可以作为FileInputStream类的构造方法的参数 InputStream File FileOutputStream String34哪些类可以作为FilterInputStream类的构造方法的参数 FilterOutputStream File InputStream RandomAccessFile35下列哪个语句可以正确地创建一个RandomAccessFile的实例? RandomAccessFile(data, r); RandomAccessFile(r, data); RandomAccessFile(data, read); RandomAccessFile(read, data);36下面哪个语句能够正确地创建一个InputStreamReader的实例? new InputStreamReader(new FileInputStream(data); new InputStreamReader(new FileReader(data); new InputStreamReader(new BufferedReader(data); new InputStreamReader(data); new InputStreamReader(System.in);37假如文件“Ran.test”不存在,在编译和执行下面程序的时候,结果如何?import java.io.*;class Test public static void main(String args) throws Exception RandomAccessFile out=new RandomAccessFile(“Ran.test”, “rw”); out.writeBytes(“Ninotchka”); 程序编译不能通过,因为RandomAccessFile没有被正确的创建。 程序编译不能通过,因为RandomAccessFile没有实现writeBytes()方法。 程序编译、运行通过,但是抛出一个IOException,因为“Ran.test”还不存在。 程序编译、运行通过,但是在创建的“Ran.test”文件中没写入任何内容。 程序编译、运行通过,并且在创建的“Ran.test”文件中写入了“Ninotchka”。38给出下列代码import java.io.*;class Write public static void main(String args) throws Exception File file=new File(“temp.test”); FileOutputStream stream=new FileOutputStream(file); /write integers here下面哪一段代码可以用来代替main()方法中的注释部分,来写入整数0到9? DataOutputStream filter=new DataOutputStream(stream);for(int i=0;i10;i+) filter.writeInt(i); for(int i=0;i10;i+)file.writeInt(i); for(int i=0;i10;i+) stream.writeInt(i); DataOutputStream filter=new DataOutputStream(stream);for(int i=0;i10;i+) filter.write(i); for(int i=0;i10;i+) stream.write(i);39当前路径下,没有Hello.txt这个文件,下面程序编译、运行,输出的结果是什么?import java.io.*;public class Test public static void main(String args) Test m=new Test(); System.out.println(m.amethod(); public int amethod() try FileInputStream dis=new FileInputStream(“Hello.txt”); catch(FileNotFoundException fne) System.out.println(“No such file found”); Return -1; catch(IOException ioe) finally System.out.println(“Doing finally”); return 0; 选择所有正确答案 No such file found No such file found,-1 No such file found,Doing finally,-1 040如何通过File类实例对象FileName来改变当前的工作路径? FileName.chdir(“DirName”); FileName.cd(“DirName”); FileName.cwd(“DirName”); The File class does not support directly changing the current directory.41文件类能够完成下列那些操作? 改变当前的路径 返回父目录的名字 删除一个文件 测试文件是二进制文件还是文本文件42下列哪些是正确的? File f=new File(“/”, “atoexec.bat”); DataInputStream d=new DataInputStream(System.in); OutputStream Writer o=new OutputStreamWriter(System.out); RandomAccessFile r=new RandomAccessFile(“OutFile”);43要求读取一个几十兆的文件,下面那种方式是最适合的? new FileInputStream(“”) new InputStreamReader(new FileInputStream(“”) new BufferedReader(new InputStreamReader(new FileInputStream(“”); new RandomAccessFile raf=new RandomAccessFile(“myfile.txt”, “rw”)44什么时候下面程序将抛出IOExceptionimport java.io.*;class Test public static void main(String args) try String strString=“Now is the time to take Sun Certification”; Char buffer=new charstrString.length(); StrString.getChars(0,strString.length(),buffer,0); FileWriter f=new FileWriter(“MyFile1.txt”); FileWriter f1=f; F1.close(); For(int I=0;Ibuffer.length;I+=2) f.write(bufferi); f.close(); FileWriter f2=new FileWriter(“MyFile2.txt”); F2.write(buffer); F2.close(); catch(IOException ie) System.out.println(ie.getMessage(); 选择正确的答案 从来不会抛IOException f1.close()所在行将抛出IOException f.write(bufferi)将抛出IOException 当产生一个FileWriter对象时45下面关于File类的说法哪些时正确的? File类能被用来创建文件和目录 File类有一个mkdir()方法用来创建目录 File类有一个mkdirs()方法用来创建目录和其父目录 File类不能用来创建目录46下面哪些FileOutputStr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论