一、下列關於內部類的說法,正確的是
A .其餘類不能夠用某個類的內部類聲明對象。
B .內部類字節碼文件的名字格式是「外嵌類名$內部類名」。
C .內部類的類體能夠聲明類變量和類方法。
D .內部類在成員位置上,所以能夠被private修飾。
答案:ABD
解析:P162。C項:內部類的類體中不能夠聲明類變量和類方法。java
二、外部類訪問內部類,必須創建內部類對象。
A .true
B .false
答案:A正則表達式
三、下面的類沒法經過編譯,第三行出現錯誤提示,由於類不能夠被static修飾。數組
class Outer{ int x = 3; static class Inner{ void function() { System.out.println(x); } } }
A .true
B .false
答案:B
解析:第五行出現錯誤提示。內部類能夠被static修飾。當內部類被static修飾時,只能直接訪問外部類的static成員。緩存
四、下列關於匿名類的敘述,錯誤的是
A .定義匿名內部類的前提是,內部類必須繼承一個類或實現接口。
B .匿名類的類體不能夠聲明static成員變量和static方法。
C .不能夠直接使用匿名類建立一個對象。
D .匿名內部類的格式爲:new 父類或接口(){定義子類的內容}。
答案:C
解析:7.2節。C項:能夠直接用匿名類建立一個對象。app
五、若是某個方法的參數是接口類型,那麼可使用接口名和類體組合建立一個匿名對象傳遞給方法的參數,類體必需要重寫接口中的所有方法。
A .true
B .false
答案:A
解析:7.2.2節。dom
六、下列關於異常的說法,錯誤的是
A .Java使用throws拋出一個異常,使用throw聲明方法可能拋出異常。
B .執行System.out.println(3/0);語句會報ArithmeticException異常。
C .Java中的錯誤是以對象的方式呈現爲java.lang.Throwable的各類子類實例。
D .方法parseInt()在執行過程當中可能拋出DataFormatException異常。
答案:AD
解析:7.3節。A項:Java使用throw拋出一個異常,使用throws聲明方法可能拋出異常。D項:可能拋出NumberFormatException異常。oop
七、若是超出JVM運行能力以外,如「byte[] arr=new byte[10241024600];」會拋出java.lang.OutOfMemoryError異常。
A .true
B .false
答案:B
解析:對於嚴重的錯誤,經過Error類來描述,而對於非嚴重的問題,則是經過Exception類來進行描述的。測試
八、下列關於異常處理的說法,正確的是
A .一旦try部分拋出異常對象,那麼try部分將馬上結束執行,轉向執行相應的catch部分。
B .catch代碼塊定義必定會執行的代碼,它一般用於關閉資源。
C .try-catch語句能夠由幾個catch組成,分別處理髮生的異常。
D .catch括號中列出的異常不得有繼承關係,不然會發生編譯錯誤。
答案:ACD
解析:7.3.1節。B項:finally代碼塊定義必定會執行的代碼,它一般用於關閉資源。對於異常的部分,若是沒有作finally處理,那麼這個程序是有缺陷的,每次調用完資源再把資源釋放掉是必須的,不然運行壓力會特別大。ui
九、下列程序的運行結果爲編碼
public class FinallyDemo { public static void main(String[] args) { System.out.print(test(true)); } static int test(boolean flag) { try { if (flag) { return 1; } } finally { System.out.print("finally…"); } return 0; } }
A .1finally…
B .finally…1
C .1finally…0
D .1
答案:B
解析:若是程序撰寫的流程中先return了,也有finally區塊,finally區塊會先執行完後,再將值返回。Finally代碼塊只有一種狀況不會被執行,就是在以前執行了System.exit(0)。
十、下列關於斷言的說法,錯誤的是
A .斷言語句一般用於調試代碼。
B .若是使用assert booleanException:messageException;形式的斷言語句,當booleanException的值是false時,程序從斷言語句處中止執行,並輸出messageException的值。
C .在調試程序時,可使用-ea啓動斷言語句。
D .String n = new AssertDemo().getName("Magical");
assertThat(n, startsWith("Ma"));沒法經過測試,而assertThat(n, startsWith(‘M’));能夠經過測試。
答案:D
解析:7.4節。D項:startsWith:字符串變量以指定字符串開頭時,測試經過。
十一、下列說法正確的是
A .內部類的外嵌類的成員變量在內部類中仍然有效。
B .內部類中的方法也能夠調用外嵌類中的方法。
C .內部類的類體中能夠聲明類變量和類方法。
D .匿名類必定是內部類。
答案:ABD
十二、下列沒法經過編譯的是
class OutClass { int m = 1; static float x; //A class InnerClass { int m =12; //B static float n =20.89f; //C InnerClass(){ } void f() { m = 100; } } void cry() { InnerClass tom = new InnerClass(); //D } }
A .A
B .B
C .C
D .D
答案:C
1三、下列說法正確的是
A .和接口有關的匿名類能夠是抽象類。
B .和類有關的匿名類還能夠額外地實現某個指定的接口。
C .和類有關的匿名類必定是該類的一個非抽象子類。
D .和接口有關的匿名類的類體中能夠有static成員變量。
答案:C
1四、調用線程的interrupt()方法 ,會拋出哪些異常對象?
A .ClosedByInterruptException
B .IllegalStateException
C .RuntimeException
D .InterruptedException
E .SecurityException
答案:ADE
解析:查詢API。
1五、下面哪行代碼插入註釋處,會致使輸出「oops」?
class Calc { public static void main(String [] args) { try { int x = Integer.parseInt ("42a") ;} catch (IllegalArgumentException e) { System.out.print ("oops"); } } }
A .catch (IllegalArgumentException e)
B .catch (IllegalStateException c)
C .catch (NumberFormatException n)
D .catch (ClassCastException c)
答案:AC
1六、下列關於自定義異常類的描述,正確的是
A .自定義異常必須繼承Exception。
B .自定義異常能夠繼承自Error。
C .自定義異常能夠更加明肯定位異常出錯的位置和給出詳細出錯信息。
D .程序中已經提供了豐富的異常類,使用自定義異常沒有意義。
答案:C
1七、已知String s = null;下列代碼會拋出NullPointerException異常的有
A .if( (s!=null) & (s.length()>0) )
B .if( (s!=null) && (s.length()>0) )
C .if( (s==null) | (s.length()==0) )
D .if( (s==null) || (s.length()==0) )
答案:AC
1八、下列程序的運行結果是
class Calc { public static void func() throws Exception{ try { throw new Exception(); } finally { System.out.println("B"); } } public static void main(String[] args) { try { func(); System.out.println("A"); }catch(Exception e) { System.out.println("C"); } System.out.println("D"); }
A .A
B .B
C .C
D .D
答案:BCD
1九、下列不能使用在throw語句中的是
A .Error
B .RuntimeException
C .Object
D .Throwable
E .Exception
答案:C
20、在java中全部的異常類都繼承自java.lang.Throwable類,它有兩個直接子類,一個是Error類,另外一個是Exception類。
A .true
B .false
答案:A
2一、下列說法正確的是
A .輸入流的指向稱爲它的源。
B .輸出流的指向稱爲它的源。
C .源和目的地也能夠是鍵盤、內存或顯示器窗口。
D .java.io包提供大量流類。
答案:ACD
解析:P281 B項:輸出流的指向稱爲它的目的地。
2二、File類的對象主要用來獲取文件自己的一些信息,如對文件的讀寫操做、獲取文件所在目錄、文件長度或文件讀寫權限等。
A .true
B .false
答案:B
解析:File類不涉及對文件的讀寫操做。
2三、如下是File類構造方法的是
A .File(File parent, String child)
B .File(String pathname)
C .File(String parent, String child)
D .File(URI uri)
答案:ABCD
解析:查詢API可知,以上均爲File類的構造方法。
2四、如下關於File類經常使用方法的敘述,錯誤的是
A .public long length():獲取文件長度
B .public int hashCode():計算此文件的哈希碼
C .public String toString():返回此抽象路徑名的路徑名字符串
D .public Boolean isFile():判斷一個文件是不是普通文件,而不是目錄
答案:B
解析:public int hashCode():計算此抽象路徑名的哈希碼。
2五、可使用public String[] list(FilenameFilter obj)或public File [] listFiles(FilenameFilter obj),列出目錄下指定類型的文件。
A .true
B .false
答案:A
2六、下列說法錯誤的是
A .某文件對象調用方法delete()能夠刪除當前文件。
B .某文件對象調用方法create()能夠建立一個特定名稱的文件。
C .Runtime類位於java.lang包
D .ec能夠調用exec(String command)方法打開本地機器上的可執行文件或一個操做。
答案:B
2七、InputStream類繼承自FileInputStream,能夠以字節爲單位讀取文件。
A .true
B .false
答案:B
解析:InputStream是父類。
2八、如下說法正確的是
A .調用FileInputStream構造方法,試圖打開一個只讀文件進行寫入,會拋出FileNotFoundException異常。
B .爲了捕獲錯誤,必須在try-catch語句以前建立輸入流,在try-catch語句之中檢測並處理這個異常。
C .調用public int read()方法,若已達到文件末尾,則返回0。
D .調用public int read()方法,若沒有輸出可用,則此方法自動返回0。
答案:A
解析:B項:在try中建立輸入流,在catch中檢測並處理這個異常。C項:返回-1。D項:則此方法阻塞。
2九、下列關於public int read(byte[] b, int off, int len)方法的敘述,正確的是
A .此方法覆蓋了InputStream類中的read方法。
B .此方法從該輸入流中將最多len個字節的數據讀入一個byte數組中。
C .此方法返回讀入緩衝區的字節總數,若是由於已經到達文件末尾而沒有更多的數據,則返回-1。
D .若是b爲null,則會拋出IndexOutOfBoundsException異常。
答案:ABC
解析:D項:會拋出NullPointerException異常。
30、只要不關閉FileInputStream流,每次調用read方法就順序地讀取源中其他的內容,直到源的末尾或流被關閉。
A .true
B .false
答案:A
解析:P286。
3一、下列說法正確的是
A .可使用FileOutputStream類寫入文件。
B .FileOutputStream類的構造方法有FileOutputStream(File file)、FileOutputStream(String name)、FileOutputStream(File file, boolean append)等。
C .對於FileOutputStream(String name)方法,參數name指定的文件稱爲輸出流的源。
D .若是輸出流指向的文件是已存在的文件,輸出流將刷新該文件,使得文件的長度爲0。
答案:ABD
解析:參數name指定的文件稱爲輸出流的目的地。
3二、FileOutputStream輸出流開通一個到達文件的通道,若是輸出流指向的文件不存在,將拋出NullPointerException異常。
A .true
B .false
答案:B
解析:若不存在,將建立該文件。
3三、如下說法錯誤的是
A .爲了更好地操做Unicode字符,可使用字符輸入/輸出流。
B .一個漢字在文件中佔4個字節。
C .調用flush()方法能夠將當前緩衝區的內容寫入目的地。
D .FileReader和FileWriter分別是Reader和Writer的子類。
答案:B
3四、正則表達式abc? 匹配
A .ab
B .abc
C .abcc
D .abccc
答案:AB
3五、下列關於BufferedReader和BufferedWriter的說法,錯誤的是
A .經過調用BufferedReader對象的readLine()方法,能夠讀取文本行。
B .BufferedReader有一個向文件寫入回行符的方法:newLine()。
C .當BufferedWriter流調用flush()刷新緩存或調用close()方法關閉時,即便緩存沒有溢出,,也會將緩存的內容寫入目的地。
D .能夠將BufferedWriter流和FileWriter流鏈接在一塊兒,而後使用BufferedWriter流將數據寫到目的地。
答案:B
3六、RandomAccessFile類建立的流的指向既能夠做爲流的源,也能夠做爲流的目的地。
A .true
B .false
答案:A
解析:P292。
3七、下列說法正確的是
A .字節數組輸入流調用public int read();能夠順序從源中讀出一個字節。
B .調用public int read(byte[] b, int off, int len);能夠順序地從源中讀出參數len指定的字節數。
C .構造方法ByteArrayOutputStream(int size)構造的字節數組輸出流指向一個默認爲32字節的緩衝區。
D .字符數組流CharArrayReader和CharArrayWriter分別使用字符數組做爲流的源和目標。
答案:ABD
解析:ByteArrayOutputStream(int size)緩衝區默認大小由size指定。
3八、下列關於數據流的說法,正確的是
A .數據輸入流容許應用程序以與機器無關方式從底層輸入流中讀取基本 Java 數據類型
B .使用DataInputStream和DataOutputStream類建立的對象來讀取數值時,沒必要再關心這個數值應當是多少個字節。
C .DataInputStream類的public final int read(byte[] b)方法,若是b爲 null,則拋出NullPointerException。
D .readUTF()的做用,是從輸入流中讀取UTF-8編碼的數據,並以String字符串的形式返回。
答案:ABCD
3九、ObjectOutputStream可以把對象寫入到輸出流中,而不須要每次寫入一個字節。可使用 writeObject 和 readObject 方法爲類重寫默認的反序列化。
A .true
B .false
答案:A
40、下列關於Scanner類的說法,正確的是
A .使用Scanner和正則表達式來解析文件的特色是以空間換取時間。
B .解析時若是單詞不是數字型單詞,調用nextInt()或nextDouble()方法將發生InputMismatchException異常。
C .建立Scanner對象,指向要解析的文件,可使用useDelimiter方法指定正則表達式做爲分割標記。
D .正則表達式\b((?!abc)\w)+\b能夠用來匹配不包含abc的單詞。
答案:BCD
解析:A項,以時間換取空間。
4一、下列哪一個敘述是正確的?
A .建立File對象可能發生異常。
B .BufferedRead流能夠指向FileInputStream流。
C .BufferedWrite流能夠指向FileWrite流。
D .RandomAccessFile流一旦指向文件,就會刷新該文件。
答案:C
4二、爲了向文件hello.txt尾加數據,下列哪一個是正確建立指向hello.txt的流?
A .try { OutputStream out = new FileOutputStream ("hello.txt");
} catch(IOException e){}
B .try { OutputStream out = new FileOutputStream ("hello.txt",true);
} catch(IOException e){}
C .try { OutputStream out = new FileOutputStream ("hello.txt",false);
} catch(IOException e){}
D .try { OutputStream out = new OutputStream ("hello.txt",true);
}
catch(IOException e){}
答案:B
4三、下列哪個不是java.io類的子類?
A .BufferedReader
B .BufferedWriter
C .FileReader
D .FileWriter
E .PrintReader
F .PrintWriter
答案:E
4四、下列選項中,能夠經過編譯的是
InputStream is = new BufferedInputStream(new FileInputStream("zoo.txt"));
InputStream wrapper = new _____;
A .BufferedInputStream
B .FileInputStream
C .BufferedWriter
D .ObjectInputStream
E .ObjectOutputStream
F .BufferedReader
答案:AD
4五、What is the result of executing the following code?
String line; Console c = System.console(); Writer w = c.writer(); if ((line = c.readLine()) != null) w.append(line); w.flush();
A .The code runs without error but prints nothing.
B .The code prints what was entered by the user.
C .An ArrayIndexOutOfBoundsException might be thrown.
D .A NullPointerException might be thrown.
E .An IOException might be thrown.
F .The code does not compile.
答案:BDE
解析:This is correct code for reading a line from the console and writing it back out to the console, making option B correct. Options D and E are also correct. If no console is available, a NullPointerException is thrown. The append() method throws anIOException.
4六、Assuming zoo-data.txt is a multiline text file, what is true of the following method?
private void echo() throws IOException { try (FileReader fileReader = new FileReader("zoo-data.txt"); BufferedReader bufferedReader = new BufferedReader(fileReader)) { System.out.println(bufferedReader.readLine()); } }
A .It prints the first line of the file to the console.
B .It prints the entire contents of the file.
C .The code does not compile because the reader is not closed.
D .The code does compile, but the reader is not closed.
E .The code does not compile for another reason.
答案:A
解析:This code compiles and runs without issue, so C and E are incorrect. It uses a try-with-resource block to open the FileReader and BufferedReader objects. Therefore, both get closed automatically, and D is incorrect. The body of the try block reads in the first line of the file and outputs it to the user. Therefore, A is correct. Since the rest of the file is not read, B is incorrect.
4七、What are some reasons to use a character stream, such as Reader/Writer, over a byte stream, such as InputStream/OutputStream? (Choose all that apply.) A .More convenient code syntax when working with String data B .Improved performance C .Automatic character encoding D .Built-in serialization and deserialization E .Character streams are high-level streams F .Multi-threading support 答案:AC 解析:Character stream classes often include built-in convenience methods for working withString data, so A is correct. They also handle character encoding automatically, so C is also correct. The rest of the statements are irrelevant or incorrect and are not properties of all character streams. (說明:P是書上頁碼,詳情請看書)