1.新建一个流对象,下面哪个选项的代码是错误的?(B )
A)new BufferedWriter(new FileWriter("a.txt")); B)new BufferedReader(new FileInputStream("a.dat")); C)new GZIPOutputStream(new FileOutputStream("a.zip")); D)new ObjectInputStream(new FileInputStream("a.dat"));
2.getCustomerInfo()方法如下,try中可以捕获三种类型的异常,如果在该方法运行中产生了一个IOException,将会输出什么结果(A)
public void getCustomerInfo() { try { // do something that may cause an Exception } catch (java.io.FileNotFoundExceptionex){ System.out.print("FileNotFoundException!"); } catch (java.io.IOExceptionex){ System.out.print("IOException!"); } catch (java.lang.Exceptionex){ System.out.print("Exception!"); } }
A)IOException!
B)IOException!Exception!
C)FileNotFoundException!IOException!
D)FileNotFoundException!IOException!Exception!
3. 下面代码的运行结果为:(C)import java.io.*; import java.util.*; public class foo{ public static void main (String[] args){ String s; System.out.println("s=" + s); } }
A 代码得到编译,并输出“s=”
B 代码得到编译,并输出“s=null”
C 由于String s没有初始化,代码不能编译通过
D 代码得到编译,但捕获到 NullPointException异常
4.指出下列程序运行的结果 (B)
public class Example { String str = new String("good"); char[] ch = { ‘a‘, ‘b‘, ‘c‘ }; public static void main(String args[]) { Example ex = new Example(); ex.change(ex.str, ex.ch); System.out.print(ex.str + " and "); System.out.print(ex.ch); } public void change(String str, char ch[]) { str = "test ok"; ch[0] = ‘g‘; } }
A、 good and abc
B、 good and gbc
C、 test ok and abc
D、 test ok and gbc
时间: 2024-10-17 13:47:48