Java 输入输出(含部分答案)

  • 格式:doc
  • 大小:116.00 KB
  • 文档页数:23

Java输入输出

知识点:

1、文件和流的操作,理解字节流和字符流的概念

2、异常处理的概念,自定义异常类

一、选择题

1、如果一个程序段中有多个catch,则程序会按如下哪种情况执行? ( )

A)找到合适的例外类型后继续执行后面的catch

B)找到每个符合条件的catch都执行一次

C)找到合适的例外类型后就不再执行后面的catch

D)对每个catch都执行一次

2、程序员将可能发生异常的代码放在( )块中,后面紧跟着一个或多个( )块。

A) catch、try B) try、catch

C) try、exception D) exception、try

3、在Java语言中,在程序运行时会自动检查数组的下标是否越界,如果越界,会抛掷下面的异常( )

A) SQLException B) IOException

C) ArrayIndexOutOfBoundsExcetion D) SecurityManager

4、在Java中,关于捕获异常的语法try-catch-finally的下列描述哪些正确?( )

A) try-catch必须配对使用 B) try可以单独使用

C) try-finally必须配对使用 D) 在try-catch后如果定义了finally,则finally肯定会执行

5、给定下面的代码片断:

public class Animal{

public void Cat(){

try{

Method();

}

catch(ArrayIndexOutBoundsException e)

{System.out.println("Exception1");}

catch(Exception e)

{System.out.println("Exception2");}

finally

{System.out.println("Hello World!!");}

}

public void Method()

{

//...

}

public static void main(String[] args)

{

Animal.Ani=new Animal ();

Ani. Cat ();

}

}

如果函数method正常运行并返回,会显示下面的哪些信息?( )

A) Hello World B) Exception1 2 C) Exception2 D) Hello World!!

6、如果下列的方法能够正常运行,在控制台上将显示什么?( )

public void example(){

try{

unsafe();

System.out.println("Test1");

}

catch(SafeException e)

{

System.out.println("Test 2");

}

finally{System.out.println("Test 3");}

System.out.println("Test 4");

}

A) Test 1 B) Test 2

C) Test 3 D) Test 4

7、以下哪一项不是File类的功能: ( )

A) 创建文件 B) 创建目录

C) 删除文件 D) 拷贝文件

2、下面哪个不是InputStream类中的方法: ( )

A) int read(byte[]) B) void flush()

C) void close() D) int available()

8、构造BufferedInputStream的合适参数是哪个?

A) BufferedInputStream B) BufferedOutputStream

C) FileInputStream D) FileOuterStream

E) File

9、要从文件" file.dat"文件中读出第10个字节到变量C中,下列哪个方法适合? ( )

A) FileInputStream in=new FileInputStream("file.dat");in.skip(9); int

c=in.read();

B) FileInputStream in=new FileInputStream("file.dat"); in.skip(10); int

c=in.read();

C) FileInputStream in=new FileInputStream("file.dat"); int c=in.read();

D) RandomAccessFile in=new RandomAccessFile("file.dat"); in.skip(9);

int c=in.readByte();

10、以下程序发生什么异常?

class A {

int x;

public static void main {

A x;

System.out.println(x.x);

}

}

A. IOException

B. InterruptException

C. NullPointerException

D. DataFormatException

11、设有如下方法:

public void test() {

try { 3 oneMethod();

System.out.println("condition 1");

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("condition 2");

} catch(Exception e) {

System.out.println("condition 3");

} finally {

System.out.println("finally");

}

}

如果oneMethod正常运行,则输出结果中有哪些?

A. condition 1

B. condition 2

C. condition 3

D. finally

12、设有如下代码:

public void fun () {

int i;

try

{

i=System.in.read ();

System.out.println("Location 1");

} catch (IOException e) {

System.out.println("Location 2");

} finally {

System.out.println("Location 3");

}

System.out.println("Location 4");

}

如果有一个IOException发生, 则输出有哪些?

A. Location 1

B. Location 2

C. Location 3

D. Location 4

13、设有如下代码:

1 String s = null;

2 if ( s != null & s.length() > 0)

3 System.out.println("s != null & s.length() > 0");

4 if ( s != null && s.length() > 0)

5 System.out.println("s != null & s.length() > 0");

6 if ( s != null || s.length() > 0)

7 System.out.println("s != null & s.length() > 0");

8 if ( s != null | s.length() > 0)

9 System.out.println("s != null | s.length() > 0");

以下行中哪些会产生空指针异常。

A. 2,4

B. 6,8

C. 2,4,6,8

D. 2,6,8 4 14、类Test1、Test2定义如下:

1.public class Test1 {

2. public float aMethod(float a,float b) throws IOException {

3. }

4. }

5. public class Test2 extends Test1{

6.

7. }

将以下哪种方法插入行6是不合法的。

A、float aMethod(float a,float b){ }

B、public int aMethod(int a,int b)throws Exception{ }

C、public float aMethod(float p,float q){ }

D、public int aMethod(int a,int b)throws IOException{ }

15、设有如下代码:

try {

tryThis();

return;

} catch (IOException x1) {

System.out.println("exception 1");

return;

} catch (Exception x2) {

System.out.println("exception 2");

return;

} finally {

System.out.println("finally");

}

如果tryThis() 抛出 NumberFormatException,则输出结果是?