一、下列关于内部类的说法,正确的是
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是书上页码,详情请看书)