JAVA考试题库第六章

合集下载

java答案第六章

java答案第六章

Java语言程序设计第六章课后习题答案1.将本章例6-1至6-18中出现的文件的构造方法均改为使用File类对象作为参数实现。

个人理解:File类只能对整文件性质进行处理,而没法通过自己直接使用file.Read()或者是file.write()类似方法对文件内容进行写或者读取。

注意:是直接;下面只提供一个例2变化,其他的你自己做,10几道啊,出这题的人真他妈有病。

import java.io.*;public class test6_2{public static void main(String[] args) throws IOException { String fileName = "D:\\Hello.txt";File writer=new File(fileName);writer.createNewFile();BufferedWriter input = new BufferedWriter(newFileWriter(writer));input.write("Hello !\n");input.write("this is my first text file,\n");input.write("你还好吗?\n");input.close();}}运行结果:(电脑系统问题,没法换行,所以一般使用BuffereWriter中newLine()实现换行)2.模仿文本文件复制的例题,编写对二进制文件进行复制的程序.// CopyMaker类import java.io.*;class CopyMaker {String sourceName, destName;BufferedInputStream source;BufferedOutputStream dest;int line;//打开源文件和目标文件,无异常返回trueprivate boolean openFiles() {try {source = new BufferedInputStream(newFileInputStream( sourceName ));}catch ( IOException iox ) {System.out.println("Problem opening " + sourceName );return false;}try {dest = new BufferedOutputStream(newFileOutputStream( destName ));}catch ( IOException iox ){System.out.println("Problem opening " + destName );return false;}return true;}//复制文件private boolean copyFiles() {try {line = source.read();while ( line != -1 ) {dest.write(line);line = source.read();}}catch ( IOException iox ) {System.out.println("Problem reading or writing" );return false;}return true;}//关闭源文件和目标文件private boolean closeFiles() {boolean retVal=true;try { source.close(); }catch ( IOException iox ) {System.out.println("Problem closing " + sourceName );retVal = false;}try { dest.close(); }catch ( IOException iox ) {System.out.println("Problem closing " + destName );retVal = false;}return retVal;}//执行复制public boolean copy(String src, String dst ) {sourceName = src ;destName = dst ;return openFiles() && copyFiles() && closeFiles();}}//test6_2public class test6_2{public static void main ( String[] args ) {String s1="lin.txt",s2="newlin.txt";if(new CopyMaker().copy(s1, s2))S ystem.out.print("复制成功");elseS ystem.out.print("复制失败");}}运行前的两个文本:lin.txt和newlin.txt(为空)运行后:3.创建一存储若干随机整数的文本文件,文件名、整数的个数及范围均由键盘输入。

JAVA第六章习题

JAVA第六章习题

第六章异常和异常处理一、选择题1、下列关于异常和异常类的描述中,错误的是 D 。

A.异常是某种异常类的对象B.异常类代表一种异常事件C.异常对象中包含有发生异常事件的类型等重要信息D.对待异常的处理就是简单地结束程序2、下列关于异常处理的描述中,错误的是 C 。

A.程序运行时出现的异常是通过系统默认的异常处理程序进行处理的。

B.在程序中可以使用try-catch语句捕捉异常和处理异常事件C.对于捕获的异常只能在当前方法中处理D.使用throw语句可将异常抛出到调用当前方法的方法中处理二、简答题1、简述Java的异常处理机制。

Java系统中定义一些用来处理异常的类,称为异常类,该类中通常包含产生某种异常的信息和处理异常的方法等内容。

当程序运行中发生了可识别的异常时(该错误有一个异常类与之相对应时)系统就会产生一个相应的该异常类的对象,简称异常。

系统中一旦产生了一个异常,便去寻找处理该种异常的处理程序,以保证不产生死机,从而保证了程序的安全运行。

这就是Java的异常处理机制.三、写出运行结果题1、public class Exam6_4{ public static void main(String args[]){ fun(0);fun(1);fun(2);fun(3);}static void fun(int i){ System.out.println("调用方法:fun"+i);try{if(i==0) System.out.println("没有异常");else if(i==1){int a=0; int b=10; b/=a;}else if(i==2){int m[]=new int[5]; m[5]=100;}else if(i==3){String str="56k9"; intn=Integer.parseInt(str);}}catch(ArithmeticException e){System.out.println("捕捉异常:"+e.getMessage());} catch(ArrayIndexOutOfBoundsException e){System.out.println("捕捉异常:"+e);}catch(NumberFormatException e){System.out.println("捕捉异常:"+e);}finally{System.out.println("处理完毕! ");}}}2、public class Exam6_5{ public static void main(String args[]){ try{fun1();}catch(ArithmeticException e){System.out.println("捕捉异常:"+e.getMessage());} try{fun2( );}catch(ArrayIndexOutOfBoundsException e){System.out.println("捕捉异常:"+e);}finally {System.out.println("处理完毕! ");}}static void fun1() throws ArithmeticException{ System.out.println("调用方法:fun1");int a=0; int b=10; b/=a;throw new ArithmeticException();}static void fun2() throws ArrayIndexOutOfBoundsException { System.out.println("调用方法:fun2");int m[]=new int[5]; m[5]=100;throw new ArrayIndexOutOfBoundsException();}}。

java考试题库第六章.docx

java考试题库第六章.docx

第六章异常和异常处理一选择题6・1 .下列关于异常的描述中,错误的是(B)A.异常是一种经过修正后程序仍可执行的错误B.异常是一种程序在运行中出现的不可恢复执行的错误C.不仅Java语言有异常处理,C++语言也有异常处理D.岀现异常不是简单结束程序,而是执行某种处理异常的代码,设法恢复程序的执行6・2.下列关于异常处理的描述中,错误的是(D)A.程序运行时异常由Java虚拟机自动进行处理B.使用try-catch-finally语句捕获异常C.使用throw语句抛出异常D.捕获到的异常只能用当前方法中处理,不能用其他方法中处理6・3.下列关于try-catch-finally语句的描述中,错误的是(A)A・try语句后面的程序段将给出处理异常的语句B・catch ()方法跟在try语句后面,它可以是一个或多个C. catch ()方法有一个参数,该参数是某种异常类的对彖D・finally语句后面的程序段总是被执行的,该语句起到提供统一接口的作用6・4.下列关于抛出异常的描述中,错误的是(D)A.捕捉到发牛的异常可在当前方法中处理,也可以抛到调用该方法的方法中处理B.在说明要抛出异常的方法吋应加关键字throw<异常列表〉C.v异常列表〉中可以有多个用逗号分隔的异常D.抛岀异常的方法中要使用下述抛出异常语句:throw<异常名〉;其中,v异常名>是异常类的类名6・5.下列关于用户创建自己的异常描述中,错误的是(D)A.创建自己的异常应先创建一个异常类B.为实现抛出异常,须在可能抛出异常的方法中书写throw语句C.捕捉异常的方法是使用try-catch-finally语句格式D.使用异常处理不会使整个系统更加安全和稳定二判断题6・1 .异常是一种特殊的运行错误的对象。

(对)62异常处理可以使整个系统更加安全和稳定。

(对)6・3.异常处理是在编译时进行的。

(错)6-4.Java语言中异常类都是ng.Throwable的子类。

java复习题库第五、六章

java复习题库第五、六章

第五章多线程选择题1. 线程调用了sleep()方法后,该线程将进入( C )状态。

A.可运行状态B.运行状态C.阻塞状态D.终止状态2. 关于java线程,下面说法错误的是(D)A.线程是以CPU为主体的行为B. java利用线程使整个系统成为异步C.创建线程的方法有两种:实现Runnable接口和继承Thread类D. 新线程一旦被创建,它将自动开始运行3.在java语言中,临界区可以是一个语句块,或者是一个方法,并用(A )关键字标识。

A.synchronizedB.includeC.importD.Thread4. 线程控制方法中,yield()的作用是(D)A.返回当前线程的引用B.使比其低的优先级线程执行C.强行终止线程D.只让给同优先级线程运行5. java用(A)机制实现了进程之间的异步执行A.监视器B.虚拟机C.多个CPUD.异步调用6. 下面代码运行的结果是什么?(D)public class MyThread implements Runnable {String myString = "Yes ";public void run() {this.myString = "No ";}public static void main(String[] args) {MyThread t = new MyThread();new Thread(t).start();for (int i=0; i < 10; i++)System.out.print(t.myString);}}A. 打印yes yes yes yes yes yes B. 打印no no no no no no no noC. 打印yes no yes no ye no ye no D. 不确定二、填空题1.____多线程____是java程序的并发机制,它能同步共享数据、处理不同的事件。

AnjoyoJava06章节考试题

AnjoyoJava06章节考试题

第六章考试题一、选择题:(每题3分,共20题)1.关于异常的含义,下列描述中最正确的一项是()。

A、程序编译或运行时发生的异常事件B、程序语法错误C、程序自定义的异常事件D、程序编译错误2.自定义异常时,可以通过对下列哪一项进行继承(A )。

A、Exception类及其子类B、Applet类C、AssertionError类D、Error类3.对应try和catch子句的排列方式,下列说法正确的一项是(B )。

A、父类和子类不能同时出现在try语句块中B、子类异常在前,父类异常在后C、父类异常在前,子类异常在后D、只能有子类异常4.运行下面程序时,会产生的异常是( D )。

public class Test06_01 {public static void main(String[] args) {int x = 0;int y = 5/x;int[] z = {1,2,3,4};int p = z[4];}}A、ArrayIndexOutOfBoundsExceptionB、IOExceptionC、NumberFormatExceptionD、ArithmeticException5.运行下面程序时,会产生的异常是(D)。

public class Test06_02 {public static void main(String[] args) {int[] z = {1,2,3,4};int p = z[4];int x = 0;int y = 5/x;}}A、NumberFormatExceptionB、ArithmeticExceptionC、IOExceptionD、ArrayIndexOutOfBoundsException6.下列程序执行的结果是()。

public class Test06_03 {public static void main(String[] args) {try{return;}finally{System.out.println("Finally");}}}A、编译通过,但运行时出现异常B、因为没有catch子句,因此不能通过编译C、程序正常运行,并输出FinallyD、程序正常运行,但不输出任何结果7.下列代码中给出正确的在方法体内抛出异常的是()。

《Java语言程序设计:基础篇》课后复习题答案-第六章

《Java语言程序设计:基础篇》课后复习题答案-第六章

《Java语言程序设计:基础篇》课后复习题答案-第六章Chapter6Single-dimensional Arrays1.See the section"Declaring and Creating Arrays."2.You access an array using its index.3.No memory is allocated when an array is declared.The memory is allocated whencreating the array.x is60The size of numbers is304.Indicate true or false for the following statements:1.Every element in an array has the same type.Answer:True2.The array size is fixed after it is declared.Answer:False3.The array size is fixed after it is created.Answer:True4.The element in the array must be of primitive data type.Answer:False5.Which of the following statements are valid array declarations?int i=new int(30);Answer:Invaliddouble d[]=new double[30];Answer:Validchar[]r=new char(1..30);Answer:Invalidint i[]=(3,4,3,2);Answer:Invalidfloat f[]={2.3, 4.5, 5.6};Answer:Validchar[]c=new char();Answer:Invalid6.The array index type is int and its lowest index is0.a[2]7.(a)double[]list=new double[10];(b)list[list.length–1]=5.5;(c)System.out.println(list[0]+list[1]);(d)double sum=0;for(int i=0;i<list.length;i++)< p="">sum+=list[i];(e)double min=list[0];for(int i=1;i<list.length;i++)< p="">if(min>list[i])min=list[i];(f)System.out.println(list[(int)(Math.random()*list.length));(g)double[]={3.5, 5.5, 4.52, 5.6};8.A runtime exception occurs.9.Line3:the array declaration is wrong.It should be double[].The array needs tobe created before its been used.e.g.new double[10]Line5:The semicolon(;)at the end of the for loop heading should be removed.Line5:r.length()should be r.length.Line6:random should be random()Line6:r(i)should be r[i].10.System.arraycopy(source,0,t,0,source.length);11.The second assignment statement myList=new int[20]creates a new array andassigns its reference to myList.myList new int[10]Array myList new int[10]Arraynew int[20]Array12.False.When an array is passed to a method,the reference value of the array ispassed.No new array is created.Both argument and parameter point to the samearray.13.numbers is 0and numbers[0]is314.(A) ExecutingcreateArray in Line 6Space required for the main methodchar[] chars: refHeap Array of 100 charactersSpace required for the createArray methodchar[] chars: ref(B) After exitingcreateArray in Line 6Space required for themain methodchar[] chars: refHeapArray of 100charactersStack Stack (C) ExecutingdisplayArray in Line 10Space required for the main methodchar[] chars: refHeap Array of 100 charactersSpace required for the displayArray method char[] chars: ref(D) After exitingdisplayArray in Line10Space required for themain methodchar[] chars: refHeapArray of 100charactersStack Stack(E) Executing countLetters in Line 13 Space required for the main methodint[] counts: refchar[] chars: refHeap Array of 100 charactersSpace required for the countLetters method int[] counts: refchar[] chars: ref (F) After exitingcountLetters in Line 13Space required for themain methodint[] counts: refchar[] chars: refHeapArray of 100charactersStack StackArray of 26 integers Array of 26 integers(G) Executing displayCounts in Line 18Space required for the main methodint[] counts: refchar[] chars: refHeap Array of 100 charactersSpace required for the displayCounts methodint[] counts: ref (H) After exitingdisplayCounts in Line 18Space required for themain methodint[] counts: refchar[] chars: refHeapArray of 100charactersStack StackArray of 26 integers Array of 26 integers15.Only one variable-length parameter may be specified ina method and this parameter must be the last parameter.The method return type cannot be a variable-length parameter.16.The last oneprintMax(new int[]{1,2,3});is incorrect,because the array must of the double[] type.17.Omitted18.Omitted19.Omitted20Simply change(currentMaxlist[j])21Simply change list[k]>currentElement on Line9tolist[k]<currentelement< p="">22.You can sort an array of any primitive types except boolean.The sort method is void,so it does not return a new array.23.To apply java.util.Arrays.binarySearch(array,key),the array must be sorted in increasing order.24.Line1:list is{2,4,7,10}Line2:list is{7,7,7,7}Line3:list is{7,8,8,7}Line4:list is{7,8,8,7}</currentelement<></list.length;i++)<></list.length;i++)<>。

java考试题库第六章

java考试题库第六章

java考试题库第六章第六章异常和异常处理一选择题6-1.下列关于异常的描述中,错误的是(B)A.异常是一种经过修正后程序仍可执行的错误B.异常是一种程序在运行中出现的不可恢复执行的错误C.不仅Java语言有异常处理,C++语言也有异常处理D.出现异常不是简单结束程序,而是执行某种处理异常的代码,设法恢复程序的执行6-2.下列关于异常处理的描述中,错误的是(D)A.程序运行时异常由Java虚拟机自动进行处理B.使用try-catch-finally语句捕获异常C.使用throw语句抛出异常D.捕获到的异常只能用当前方法中处理,不能用其他方法中处理6-3.下列关于try-catch-finally语句的描述中,错误的是(A)A.try语句后面的程序段将给出处理异常的语句B.catch()方法跟在try语句后面,它可以是一个或多个C.catch()方法有一个参数,该参数是某种异常类的对象D.finally语句后面的程序段总是被执行的,该语句起到提供统一接口的作用6-4.下列关于抛出异常的描述中,错误的是(D)A.捕捉到发生的异常可在当前方法中处理,也可以抛到调用该方法的方法中处理B.在说明要抛出异常的方法时应加关键字throw<异常列表>C.<异常列表>中可以有多个用逗号分隔的异常D.抛出异常的方法中要使用下述抛出异常语句:throw<异常名>;其中,<异常名>是异常类的类名6-5.下列关于用户创建自己的异常描述中,错误的是(D)A.创建自己的异常应先创建一个异常类B.为实现抛出异常,须在可能抛出异常的方法中书写throw语句C.捕捉异常的方法是使用try-catch-finally语句格式D.使用异常处理不会使整个系统更加安全和稳定二判断题6-1.异常是一种特殊的运行错误的对象。

(对)6-2.异常处理可以使整个系统更加安全和稳定。

(对)6-3.异常处理是在编译时进行的。

Java实战经典(第六章课后题答案)

Java实战经典(第六章课后题答案)
super(name,age,sex); this.yearsalary=yearsalary; this.post=post; } public String Info() { return ()+",工资:"+this.yearsalary+",职位:"+this.post; } } class Staff extends Employee { private float monthsalary; private String post; public Staff(String name,int age,String sex,float monthsalary,String post) { super(name,age,sex); this.monthsalary=monthsalary; this.post=post; } public String Info() { return ()+",工资:"+this.monthsalary+",职位: "+this.post; } } public class Six04
public float getFoot(){ return this.foot ;
} public float getHeight(){
return this.height ; } } class Cycle extends Shape{ private float radius ; private static final float PI = 3.1415926f ; public Cycle(){} public Cycle(float radius){
4
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
6-5.下列关于用户创建自己的异常描述中,错误的是(D) A.创建自己的异常应先创建一个异常类 B.为实现抛出异常,须在可能抛出异常的方法中书写 throw 语句 C.捕捉异常的方法是使用 try-catch-finally 语句格式 D.使用异常处理不会使整个系统更加安全和稳定
二 判断题
6-1.异常是一种特殊的运行错误的对象。(对) 6-2.异常处理可以使整个系统更加安全和稳定。(对) 6-3.异常处理是在编译时进行的。(错) 6-4.Java 语言中异常类都是 ng.Throwable 的子类。(对) 6-5.Throwable 类有两个子类:Error 类和 Exception 类。前者由系统保留,后者供应用程序使用。(对) 6-6.异常通常是指 Error 类和 Exception 类。(错) 6-7.Exception 类只有一个子类为 RuntimeException。(错) 6-8.在异常处理中,出现异常和抛出异常是一回事。(错) 6-9.运行时异常是在运行时系统检测并处理的。(错) 6-10.使用 try-catch-finally 语句只能捕获一个异常。(错) 6-11.捕获异常时 try 语句后面通常跟有一个或多个 catch()方法用来处理 try 块内生成的异常事件。(对) 6-12.使用 finally 语句的程序代码为该程序提供一个统一的的出口。(对) 6-13.抛出异常的方法说明中要加关键字 throws,并在该方法中还应添加 throw 语句。(对) 6-14.创建异常类时要给出该异常类的父类。(对) 6-15.如果异常类没有被捕获将会产生不正常的终止。(对) 三 分析程序的输出结果
6-1.Exer6_1.java public class Exer6_1 { public static void main(String args[] )
{ int x=10,y=0; int z=x/y; System.out.println(“z=“+z);
} } 该程序运行后,输出结果如图所示:
(1) 数组下标越界异常 ArrayIndexOutOfBoundsException 类型。例如, char ch[]=new char[5]; ch[5]='m';//产生该类型异常
(2) 对象转换异常 ClassCastException 类型。 例如,将对象 a1 转换为对象 a2 时,如果 a1 和 a2 不是同类,并且 a1 也不是 a2 的子类对
D:\JAVA\XT>java Exer6_3 正常:d=16 异常处理结束!
异常:ng.ArithmeticException:/by zero 异常处理结束!
正常:d=12 异常处理结束!
异常:ng.ArrayIndexOutOfBoundsExcepiton:3 异常处理结束!
D:\JAVA\XT>java Exer6_1 Exception in thread”main”ng.ArithmeticException:/by zero
At Exer6_1.main<Exer6_1.java:6> 6-2.Exer6_2.java public class Exer6_2 {
6-4 Exer6_4.java public class Exer6_4 {
public static void Test() {
int a[]=new int[3]; for(int i=0;i<=a.length;i++) {
try {
a[i]=i+5; System.out.println("\t 正常:a["+i+"]="+a[i]); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("\t 异常:+e.toString()"); throw e; } } } public static void main(String args[]) { try { Test(); } finally { System.out.println("\t 异常处理结束!\n") } } } 运行该程序后,输出结果如图:
D:\JAVA\XT>java Exer6_4 正常:a[0]=5 正常:a[1]=6 正常:a[2]=7 异常:ng.ArrayIndexOutOfBoudsException: 3 异常处理结束!
Exception in thread “main” ng.ArrayIndexOutOfBoundsExcepiton:3 at Exer6_4.Test<Exer6_4.java:10> at Exer6_4.main<Exer6_4java:24>
第六章 异常和异常处理
一 选择题
6-1.下列关于异常的描述中,错误的是(B) A.异常是一种经过修正后程序仍可执行的错误 B.异常是一种程序在运行中出现的不可恢复执行的错误 C.不仅 Java 语言有异常处理,C++语言也有异常处理 D.出现异常不是简单结束程序,而是执行某种处理异常的代码,设法恢复程序的执行
运行该程序后,输出如图所示
D:\JAVA\XT>java Exer6_2 正常:d=16 异常处理结束!
算术异常! 异常处理结束!
正常:d=12 异常处理结束!
下标越界异常! 异常处理结束!
6-3 Exer6_3.java public class Exer6_3 { public static void main(String args[]) { int array1[]={6,0,8}; for(int i=0;i<=array1.length;i++) { try { int d=100/array1[i]; System.out.println("\t 正常:d="+d); } catch(RuntimeException e) { System.out.println("\t 异常:"+e.toString()); } finally { System.out.println("\t 异常处理结束!\n"); } } } } 运行该程序后,输出结果如图所示:
自定义异常出现的次数:2 myException:自定义异常
自定义异常出现的次数:3 myException:自定义异常
5.简单回答题 6-1 检测异常事件必须使用什么语句?
答:try 语句 6-2 catch()方法的作用是什么?该方法用户能否调用?
答: 用来处理 try 块中检测出的异常事件。 6-3 catch()方法中异常类型应与什么相符?
6-4.下列关于抛出异常的描述中,错误的是(D) A.捕捉到发生的异常可在当前方法中处理,也可以抛到调用该方法的方法中处理 B.在说明要抛出异常的方法时应加关键字 throw<异常列表> C.<异常列表>中可以有多个用逗号分隔的异常 D.抛出异常的方法中要使用下述抛出异常语句:throw<异常名>;其中,<异常名>是异常类的类名
象时,则产生该类型异常。
(3) 引用空对象的变量和方法时产生 NullPointerException 异常类型。例如, int ar[]=null; System.out.println(ar.length);//产生该类异常
(1) 关于数组下标越界异常 ArrayIndexOutOfBoundsException 请参照本书本章例 6.1 的程序,请读 者自行编写。
Object obj=new Object(); int arr[]=(int[])(obj); } } (3) 编写处理异常程序如下: public class pro6_2 { public stati[]) { try{ int ar[]=null; System.out.println(ar.length); } catch(NullPointerException e) { System.out.println("Exception:"+e); } } } 运行该程序后,输出显示结果如下:
答:与生成的异常事件类型相符。 6-4 异常示被捕获到将会出现什么现象?
答:try 语句后边的惯常处理代码将不被执行,通常使用 finally 语句提供一个统一出口。 6-5 在一个 try 语句的程序代码块中可以捕获多个异常吗?
答:可以 6-6 使用 throw 语句抛出的是类型还是对象?
答:抛出的是对象。 6-7 捕获到的异常还可以再抛出吗?
public static void main(String args[]) {
int array1[]={6,0,8}; for(int i=0;i<array1.length;i++) {
try {
int d=100/array[i]; System.out.println(“\t 正常:d=“+d); } catch(ArithmeticException e) { System.out.println("\t 算术异常"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("\t 下标越界异常"); } finally { System.out.println("\t 异常处理结束!\n"); } } } }
(2) 下面关于对象转换的例子。程序内容如下:
public class pro6_1 { public static void main(String args[]) {
相关文档
最新文档