標籤:
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
JAVA面試題(1)