第六周测试总结分析

1、下列关于内部类的说法,正确的是
A .其他类不可以用某个类的内部类声明对象。
B .内部类字节码文件的名字格式是“外嵌类名$内部类名”。
C .内部类的类体可以声明类变量和类方法。
D .内部类在成员位置上,因此可以被private修饰。
答案:ABD
解析:P162。C项:内部类的类体中不可以声明类变量和类方法。

2、外部类访问内部类,必须建立内部类对象。
A .true
B .false
答案:A

3、下面的类无法通过编译,第三行出现错误提示,因为类不可以被static修饰。

class Outer{

 int x = 3;

 static class Inner{

  void function() {

   System.out.println(x);

  }

 }

}

A .true
B .false
答案:B
解析:第五行出现错误提示。内部类可以被static修饰。当内部类被static修饰时,只能直接访问外部类的static成员。

4、下列关于匿名类的叙述,错误的是
A .定义匿名内部类的前提是,内部类必须继承一个类或实现接口。
B .匿名类的类体不可以声明static成员变量和static方法。
C .不可以直接使用匿名类创建一个对象。
D .匿名内部类的格式为:new 父类或接口(){定义子类的内容}。
答案:C
解析:7.2节。C项:可以直接用匿名类创建一个对象。

5、如果某个方法的参数是接口类型,那么可以使用接口名和类体组合创建一个匿名对象传递给方法的参数,类体必须要重写接口中的全部方法。
A .true
B .false
答案:A
解析:7.2.2节。

6、下列关于异常的说法,错误的是
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异常。

7、如果超出JVM运行能力之外,如“byte[] arr=new byte[10241024600];”会抛出java.lang.OutOfMemoryError异常。
A .true
B .false
答案:B
解析:对于严重的错误,通过Error类来描述,而对于非严重的问题,则是通过Exception类来进行描述的。

8、下列关于异常处理的说法,正确的是
A .一旦try部分抛出异常对象,那么try部分将立刻结束执行,转向执行相应的catch部分。
B .catch代码块定义一定会执行的代码,它通常用于关闭资源。
C .try-catch语句可以由几个catch组成,分别处理发生的异常。
D .catch括号中列出的异常不得有继承关系,否则会发生编译错误。
答案:ACD
解析:7.3.1节。B项:finally代码块定义一定会执行的代码,它通常用于关闭资源。对于异常的部分,如果没有做finally处理,那么这个程序是有缺陷的,每次调用完资源再把资源释放掉是必须的,否则运行压力会特别大。

9、下列程序的运行结果为

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)。

10、下列关于断言的说法,错误的是
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:字符串变量以指定字符串开头时,测试通过。

11、下列说法正确的是
A .内部类的外嵌类的成员变量在内部类中仍然有效。
B .内部类中的方法也可以调用外嵌类中的方法。
C .内部类的类体中可以声明类变量和类方法。
D .匿名类一定是内部类。
答案:ABD

12、下列无法通过编译的是

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

13、下列说法正确的是
A .和接口有关的匿名类可以是抽象类。
B .和类有关的匿名类还可以额外地实现某个指定的接口。
C .和类有关的匿名类一定是该类的一个非抽象子类。
D .和接口有关的匿名类的类体中可以有static成员变量。
答案:C

14、调用线程的interrupt()方法 ,会抛出哪些异常对象?
A .ClosedByInterruptException
B .IllegalStateException
C .RuntimeException
D .InterruptedException
E .SecurityException
答案:ADE
解析:查询API。

15、下面哪行代码插入注释处,会导致输出“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

16、下列关于自定义异常类的描述,正确的是
A .自定义异常必须继承Exception。
B .自定义异常可以继承自Error。
C .自定义异常可以更加明确定位异常出错的位置和给出详细出错信息。
D .程序中已经提供了丰富的异常类,使用自定义异常没有意义。
答案:C

17、已知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

18、下列程序的运行结果是

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

19、下列不能使用在throw语句中的是
A .Error
B .RuntimeException
C .Object
D .Throwable
E .Exception
答案:C

20、在java中所有的异常类都继承自java.lang.Throwable类,它有两个直接子类,一个是Error类,另一个是Exception类。
A .true
B .false
答案:A

21、下列说法正确的是
A .输入流的指向称为它的源。
B .输出流的指向称为它的源。
C .源和目的地也可以是键盘、内存或显示器窗口。
D .java.io包提供大量流类。
答案:ACD
解析:P281 B项:输出流的指向称为它的目的地。

22、File类的对象主要用来获取文件本身的一些信息,如对文件的读写操作、获取文件所在目录、文件长度或文件读写权限等。
A .true
B .false
答案:B
解析:File类不涉及对文件的读写操作。

23、以下是File类构造方法的是
A .File(File parent, String child)
B .File(String pathname)
C .File(String parent, String child)
D .File(URI uri)
答案:ABCD
解析:查询API可知,以上均为File类的构造方法。

24、以下关于File类常用方法的叙述,错误的是
A .public long length():获取文件长度
B .public int hashCode():计算此文件的哈希码
C .public String toString():返回此抽象路径名的路径名字符串
D .public Boolean isFile():判断一个文件是否是普通文件,而不是目录
答案:B
解析:public int hashCode():计算此抽象路径名的哈希码。

25、可以使用public String[] list(FilenameFilter obj)或public File [] listFiles(FilenameFilter obj),列出目录下指定类型的文件。
A .true
B .false
答案:A

26、下列说法错误的是
A .某文件对象调用方法delete()可以删除当前文件。
B .某文件对象调用方法create()可以创建一个特定名称的文件。
C .Runtime类位于java.lang包
D .ec可以调用exec(String command)方法打开本地机器上的可执行文件或一个操作。
答案:B

27、InputStream类继承自FileInputStream,可以以字节为单位读取文件。
A .true
B .false
答案:B
解析:InputStream是父类。

28、以下说法正确的是
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项:则此方法阻塞。

29、下列关于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。

31、下列说法正确的是
A .可以使用FileOutputStream类写入文件。
B .FileOutputStream类的构造方法有FileOutputStream(File file)、FileOutputStream(String name)、FileOutputStream(File file, boolean append)等。
C .对于FileOutputStream(String name)方法,参数name指定的文件称为输出流的源。
D .如果输出流指向的文件是已存在的文件,输出流将刷新该文件,使得文件的长度为0。
答案:ABD
解析:参数name指定的文件称为输出流的目的地。

32、FileOutputStream输出流开通一个到达文件的通道,如果输出流指向的文件不存在,将抛出NullPointerException异常。
A .true
B .false
答案:B
解析:若不存在,将创建该文件。

33、以下说法错误的是
A .为了更好地操作Unicode字符,可以使用字符输入/输出流。
B .一个汉字在文件中占4个字节。
C .调用flush()方法可以将当前缓冲区的内容写入目的地。
D .FileReader和FileWriter分别是Reader和Writer的子类。
答案:B

34、正则表达式abc? 匹配
A .ab
B .abc
C .abcc
D .abccc
答案:AB

35、下列关于BufferedReader和BufferedWriter的说法,错误的是
A .通过调用BufferedReader对象的readLine()方法,可以读取文本行。
B .BufferedReader有一个向文件写入回行符的方法:newLine()。
C .当BufferedWriter流调用flush()刷新缓存或调用close()方法关闭时,即使缓存没有溢出,,也会将缓存的内容写入目的地。
D .可以将BufferedWriter流和FileWriter流连接在一起,然后使用BufferedWriter流将数据写到目的地。
答案:B

36、RandomAccessFile类创建的流的指向既可以作为流的源,也可以作为流的目的地。
A .true
B .false
答案:A
解析:P292。

37、下列说法正确的是
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指定。

38、下列关于数据流的说法,正确的是
A .数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型
B .使用DataInputStream和DataOutputStream类创建的对象来读取数值时,不必再关心这个数值应当是多少个字节。
C .DataInputStream类的public final int read(byte[] b)方法,如果b为 null,则抛出NullPointerException。
D .readUTF()的作用,是从输入流中读取UTF-8编码的数据,并以String字符串的形式返回。
答案:ABCD

39、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项,以时间换取空间。

41、下列哪个叙述是正确的?
A .创建File对象可能发生异常。
B .BufferedRead流可以指向FileInputStream流。
C .BufferedWrite流可以指向FileWrite流。
D .RandomAccessFile流一旦指向文件,就会刷新该文件。
答案:C

42、为了向文件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

43、下列哪一个不是java.io类的子类?
A .BufferedReader
B .BufferedWriter
C .FileReader
D .FileWriter
E .PrintReader
F .PrintWriter
答案:E

44、下列选项中,可以通过编译的是

InputStream is = new BufferedInputStream(new FileInputStream("zoo.txt"));

InputStream wrapper = new _____;

A .BufferedInputStream
B .FileInputStream
C .BufferedWriter
D .ObjectInputStream
E .ObjectOutputStream
F .BufferedReader
答案:AD

45、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.

46、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.

47、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是书上页码,详情请看书)

猜你喜欢

转载自www.cnblogs.com/cjy-123/p/10817741.html