更多优质自考资料尽在百度贴吧自考乐园俱乐部
(http://tieba.http://www.wodefanwen.com//club/5346389)欢迎?加入...欢迎?交流...止不住的惊喜等着你.........
{
System.out.println(\发生了ArithmeticException异常\ }
catch(AarryIndexOutOfBoundsException e)//不是匹配的异常,且不会再捕获 //异常,如果发生数组索引超出范围所产生的异常,将执行该catch块。 {
System.out.println(\发生了AarryIndexOutOfBoundsException异常\ }
catch(Exception e)
//前两个catch块均无法捕获try块中发生的异常时,才会执行该catch块。 {
System.out.println(\发生了异常\ } finally
{ System.out.println(\执行d Finally\ } }
7.答:无论是出于何种原因,只要执行离开try/catch代码块,就会执行finally代码块。即无论try是否正常结束,都会执行 finally定义的最后的代码。如果try代码块中的任何代码或它的任何catch语句从方法返回,也会执行finally代码块。但本题中在try代 码块中执行了“System.exit(0);”语句,执行了这一语句后,Java虚拟机(JVM)将被终止。那么finally语句块一定不会被执行。
8.答:在某些情况下,如果一个方法产生自己不处理或无法处理的异常,它就必须在throws子句中声明该异常。
9.答:通常,可以采用两种方式来报告错误:返回值和异常。在Java中异常处理很常用,虽然返回错误代码在某些情况下是一种有效的方式,但是异常处理可以提供强大的结构化方法来处理异常。所以需要处理代码中的错误时就可以采用异常的方法进行处理。
10.答:ExceptionExam不是继承(扩展)Throwable的子类,只有Throwable的子类可以由throw抛出。所以,这一段代码会出错。
11.
public class ExceptionExam1 {
public static void main(String [] args) { try
{int i=0; i=3/i; }
catch(ArithmeticException e) {
System.out.println(\异常是:\ }
finally {
更多优质自考资料尽在百度贴吧自考乐园俱乐部
(http://tieba.http://www.wodefanwen.com//club/5346389)欢迎?加入...欢迎?交流...止不住的惊喜等着你.........
System.out.println(\语句被执行\ } } }
注意:如果在catch 语句中声明的异常类是Exception,catch 语句也能正确地捕获,这是因为Exception 是ArithmeticException 的父类。如果不能确定会发生哪种情况的异常,那么最好指定catch的参数为Exception,即说明异常的类型为Exception。
12.
class Excp1 extends Exception {}
class Excp2 extends Excp1 {}
public class ExceptionExam7 {
public static void main(String [] args) throws Exception { try {
throw new Excp2(); }
catch(Excp2 e) {
System.out.println(\ throw new Excp1(); }
catch(Excp1 e) {
System.out.println(\ throw new Exception(); }
catch(Exception e) {
System.out.println(\ }
finally {
System.out.println(\ } } }
说明:自定义异常类,关键是选择继承的超类——必须是Exception或者其子类。用异常代表错误,而不要再使用方法返回值。
13.答:所谓流是指同一台计算机或网络中不同计算机之间有序运动着的数据序列,Java把这些不同来源和目标的数据都统一抽象为数据流。数据流可分为输入流和输出流,输
更多优质自考资料尽在百度贴吧自考乐园俱乐部
(http://tieba.http://www.wodefanwen.com//club/5346389)欢迎?加入...欢迎?交流...止不住的惊喜等着你.........
入流代表从其他设备流入计算机的数据序列,输出流代表从计算机流向外部设备的数据序列。
流式输入输出的特点是数据的获取和发送沿数据序列的顺序进行,即每一个数据都必须等待排在它前面的数据,等前面的数据读入或送出之后才能被读写。所以流和队列一样,只能以“先进先出”的方式对其中的数据进行读写,而不能随意选择读写的位置。
14.答:两者都作为Object类的直接子类,基本输入流(无效Stream)和基本输出流(OutputStream)是处理以8位字节为基本单位的字节流类;Reader和Writer类是专门处理16位字符流的类。
15.答:构造函数有:
(1)public File(String pathname):创建一个对应于参数pathname的File类对象。参数pathname是包含目录和文件名的字符串。如果没有文件名,则代表目录。
(2)public File(String parent , String child):该构造函数将pathname分成两部分parent和child,参数parent表示目录或文件所在路径,参数child表示目录或文件名称。
(3)public File(File parent, String child):该构造函数与上面一个不同在于将parent的参数类型由String变为File,代表parent是一个已经创建的File类文件对象(指向目录)。
常用方法有:
(1)public boolean canWrite( ):返回文件是否可写。 (2)public boolean canRead( ):返回文件是否可读。
(3)public boolean createNewFile( ):当文件不存在时创建文件。 16.
import java.io.*; class CopyFile{
public static void main(String[] args) { String file1,file2 ; int ch = 0 ; try {
file1=args[0]; file2=args[1];
File无效Stream fis = new File无效Stream(file1); FileOutputStream fos=new FileOutputStream(file2); while ((ch=fis.read())!=-1) fos.write(ch); fis.close(); fos.close(); }
catch(FileNotFoundException e){
System.out.println(\源文件:未找到!\
}catch(ArrayIndexOutOfBoundsException e){ System.out.println(\缺少运行参数!\ System.exit(-1); }
catch (IOException e){
System.out.println(e.toString()); }
更多优质自考资料尽在百度贴吧自考乐园俱乐部
(http://tieba.http://www.wodefanwen.com//club/5346389)欢迎?加入...欢迎?交流...止不住的惊喜等着你.........
} }
17.答:
import java.io.*;
public class NewFile{
public static void main(String args[]){ File f=new File(\
if(f.exists()&&f.isDirectory())
System.err.println(\目录:\已经存在!\ else{
if(f.mkdir()){
System.out.println(\目录\创建结束!\ File f2=new File(f,\ try{
f2.createNewFile(); f2.setReadOnly();
}catch(IOException e){
System.out.println(e.toString()); }
System.out.println(\文件:\创建结束!\ } else
System.out.println(\目录\创建失败!\ } } } 18.答:要实现对文件的随机读取,也就是在文件的任何位置执行读写数据的操作,需要一个指针来指定读写的位置。在创建 RandomAccessFile类对象的同时,系统自动创建了一个指向这个文件开头的指针,当执行读写操作后,指针自动指向被读写数据之后的第一个字节 处。指针初始值是0,每读/写一个字节,指针自动增加1。RandomAccessFile类提供了一些控制指针移动的方法。
public long getFilePointer();获取当前指针指向文件的位置。考试大论坛 pulbic void seek(long pos);将指针移动到参数pos指定的位置。 public int skipBytes(int n);指针从当前位置向后移动n个字节位置,并返回指针实际移动的字节数。
19.答:
import java.io.*; public class Count{
public static void main(String[] args) {
int x=0,y=0,z=0; int ch; try{
while((ch=System.in.read())!='\\r'){
更多优质自考资料尽在百度贴吧自考乐园俱乐部
(http://tieba.http://www.wodefanwen.com//club/5346389)欢迎?加入...欢迎?交流...止不住的惊喜等着你.........
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z') x++;
else if(ch>='0'&&ch<='9') y++; else z++; }
}catch(IOException e){
System.out.println(e.toString()); }
System.out.println(\英文字母:\System.out.println(\数字字符:\System.out.println(\其它字符:\} }
20.答:
import java.io.*; public class InFile{
public static void main(String[] args) {
int ch; try{
FileOutputStream out=new FileOutputStream(\while((ch=System.in.read())!='\\r'){ System.out.write(ch); out.write(ch); }
out.close();
System.out.write('\\n'); }catch(IOException e){
System.out.println(e.toString()); }
System.out.println(\输出至文件完毕!\} }
21.答:
import java.io.*; public class Sort{
public static void main(String args[]) {
int a[]=new int[10]; byte b[]=new byte[10]; int t;
String str;