Introduction to Java Programming 的最后一章,完结撒花!
Chapter 17 Binary I/O

Section 17.2 How is I/O Handled in Java?
1. Which of the following statements are true?
a. A File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing.
b. You can use the PrintWriter class for outputting text to a file.
c. You can use the Scanner class for reading text from a file.
d. An input object is also called an input stream.
e. An output object is also called an output stream.
Key:abcde
File对象封装了文件或路径属性,但是不包含从/向文件读/写数据的方法

使用Scanner类读取文本数据,使用PrintWriter类写文本数据

输入对象从文件中读取数据流,输出对象将数据流写入文件。

输入对象也称作输入流,输出对象也称作输出流。

#
Section 17.3 Text I/O vs. Binary I/O
2. Which of the following statements are true?
a. Text I/O is built upon binary I/O to provide a level of abstraction for character encoding and decoding.
b. Text I/O involves encoding and decoding.
c. Binary I/O does not require conversions.
d. Binary I/O is more efficient than text I/O, because binary I/O does not require encoding and decoding.
e. Binary files are independent of the encoding scheme on the host machine and thus are portable.
Key:abcde

文本I/O建立在二进制I/O之上,为字符编码和解码提供了一个抽象级别。
文本I/O涉及编码和解码。
二进制I/O不需要转换。
二进制I/O比文本I/O更有效,因为二进制I/O不需要编码和解码。
二进制文件与主机的编码方案无关,因此是可移植的。

#
Section 17.4 Binary I/O Classes

注意这张图所描述的类之间的关系。

3.    Which method can you use to find out the number of the bytes in a file using InputStream?
a.    length()
b.    available()
c.    size()
d.    getSize()
Key:b

#
4. Which of the following statements are true?
a. All methods in FileInputStream/FileOutputStream are inherited from InputStream/OutputStream.
b. You can create a FileInputStream/FileOutputStream from a File object or a file name using FileInputStream/FileOutputStream constructors.
c. The return value -1 from the read() method signifies the end of file.
d. A java.io.FileNotFoundException would occur if you attempt to create a FileInputStream with a nonexistent file.
e. A java.io.FileNotFoundException would occur if you attempt to create a FileOutputStream with a nonexistent file.
Key:abcd

#
5. To append data to an existing file, use _____________ to construct a FileOutputStream for file out.dat.
a. new FileOutputStream("out.dat")
b. new FileOutputStream("out.dat", false)
c. new FileOutputStream("out.dat", true)
d. new FileOutputStream(true, "out.dat")
Key:c

#        
6.    What does the following code do?

FileInputStream fis = new FileInputStream("test.dat");
a.    It creates a new file named test.dat if it does not exist and opens the file so you can write to it.
b.    It creates a new file named test.dat if it does not exist and opens the file so you can write to it and read from it.
c.    It creates a new file named test.dat regardless of whether it exists or not and opens the file so you can write to it.
d.    It creates a new file named test.dat regardless of whether it exists or not and opens the file so you can write to it and read from it.
e. It creates a FileInputStream for test.dat if test.dat exists.
Key:e

#        
7.    Which type of exception occurs when creating a DataInputStream for a nonexistent file?
a.    FileNotExist
b.    FileNotExistException
c.    FileNotFound
d.    FileNotFoundException
Key:d

#        
8.    Which of the following statements is correct to create a DataOutputStream to write to a file named out.dat?
a.    DataOutputStream outfile = new DataOutputStream(new File("out.dat"));
b.    DataOutputStream outfile =  new DataOutputStream(new FileOutputStream("out.dat"));
c.    DataOutputStream outfile = new DataOutputStream(FileOutputStream("out.dat"));
d.    DataOutputStream outfile =  new DataOutputStream("out.dat");
Key:b

#
9. After the following program is finished, how many bytes are written to the file t.dat?

import java.io.*;

public class Test {
public static void main(String[] args) throws IOException {
DataOutputStream output = new DataOutputStream(
new FileOutputStream("t.dat"));
output.writeShort(1234);
output.writeShort(5678);
output.close();
}
}

a. 2 bytes.
b. 4 bytes.
c. 8 bytes.
d. 16 bytes.
Key:b Each short number takes 2 bytes. So total is 4 bytes.

#
10. After the following program is finished, how many bytes are written to the file t.dat?

import java.io.*;

public class Test {
public static void main(String[] args) throws IOException {
DataOutputStream output = new DataOutputStream(
new FileOutputStream("t.dat"));
output.writeChar('A');
output.close();
}
}

a. 2 bytes.
b. 4 bytes.
c. 8 bytes.
d. none of the above.
Key:a Two bytes of Unicode for 'A' is written

#
11. After the following program is finished, how many bytes are written to the file t.dat?

import java.io.*;

public class Test {
public static void main(String[] args) throws IOException {
DataOutputStream output = new DataOutputStream(
new FileOutputStream("t.dat"));
output.writeChars("ABCD");
output.close();
}
}

a. 2 bytes.
b. 4 bytes.
c. 8 bytes.
d. 12 bytes.
e. 16 bytes.
Key:c Two bytes of Unicode for each character is written to output.

#
12. After the following program is finished, how many bytes are written to the file t.dat?

import java.io.*;

public class Test {
public static void main(String[] args) throws IOException {
DataOutputStream output = new DataOutputStream(
new FileOutputStream("t.dat"));
output.writeUTFString("ABCD");
output.close();
}
}

以UTF格式写一个字符串s

a. 2 bytes.
b. 4 bytes.
c. 6 bytes.
d. 8 bytes.
e. 10 bytes.
Key:c "ABCD" are ASCII code, so each takes one byte in UTF. Total is 6 because the first two bytes stores the number of characters in the string.

头两个字节存储的是字符串中的字符个数。

#
13. Which of the following statements are true?
a. All files are stored in binary format. So, all files are essentially binary files.
b. Text I/O is built upon binary I/O to provide a level of abstraction for character encoding and decoding.
c. Encoding and decoding are automatically performed by text I/O.
d. For binary input, you need to know exactly how data were written in order to read them in correct type and order.
Key:abcd
所有文件都以二进制格式存储。所以,所有的文件本质上都是二进制文件。
文本I/O建立在二进制I/O之上,为字符编码和解码提供了一个抽象级别。
编码和解码由文本I/O自动执行。
对于二进制输入,需要确切地知道数据是如何写入的,以便以正确的类型和顺序读取它们。

#
Section 17.6 Object I/O
14. Which of the following statements are true?
a. ObjectInputStream/ObjectOutputStream enables you to perform I/O for objects in addition for primitive type values and strings.
b. Since ObjectInputStream/ObjectOutputStream contains all the functions of DataInputStream/DataOutputStream, you can replace DataInputStream/DataOutputStream completely by ObjectInputStream/ObjectOutputStream.
c. To write an object, the object must be serializable.
d. The Serializable interface does not contain any methods. So it is a mark interface.
e. If all the elements in an array is serializable, the array is serializable too.
Key:abcde

#
15. The Loan class given in the text does not implement java.io.Serializable. Analyze the following code.

public class Foo implements java.io.Serializable {
private int v1;
private static double v2;
private Loan v3 = new Loan();
}

a. An instance of Foo can be serialized because Foo implements Serializable.
b. An instance of Foo cannot be serialized because Foo contains a non-serializable instance variable v3.
c. If you mark v3 as transient, an instance of Foo is serializable.
Key:bc An object may not be serialized even though its class implements java.io.Serializable, because it may contain non-serializable instance variables.

#
16. Which of the following statements is not true?

a. A static variable is not serialized.
b. A transient variable is not serialized.
c. An object must be an instance of Serializable or Externalizable for it to be serialized.
d. The methods in an object are serialized.
e. All of the above are true.
Key:d
方法不能序列化
#        
Section 17.7 Random Access Files
17.    To create a file, you can use __________.
a.    FileOutputStream
b.    FileWriter
c.    RandomAccessFile
d.    All of the above.
Key:d

#        
18.    Which of the following is the legal mode for creating a new RandomAccessFile stream?
a.    "w"
b.    'r'
c.    "rw"
d.    "rwx"
Key:c

如果文件不存在则不能够使用模式"r"了,必须使用模式“rw”

#        
19.    What happens if the file test.dat does not exist when you attempt to compile and run the following code?

import java.io.*;

class Test  {
public static void main(String[] args) {
try {
RandomAccessFile raf =
new RandomAccessFile("test.dat", "r");
int i = raf.readInt();
}
catch(IOException ex) {
System.out.println("IO exception");
}
}
}

a.    The program does not compile because raf is not created correctly.
b.    The program does not compile because readInt() is not implemented in RandomAccessFile.
c.    The program compiles, but throws IOException because the file test.dat doesn't exist. The program displays IO exception.
d.    The program compiles and runs fine, but nothing is displayed on the console.
Key:c The problem is in line: new RandomAccessFile("test.dat", "r"); Because the file does not exist, you cannot open it for reading.

#
20. With which I/O class can you append or update a file?

a. RandomAccessFile(),
b. OutputStream()
c. DataOutputStream()
d. None of the above
Key:a

Java题库——Chapter17 二进制I/0的更多相关文章

  1. Java题库——Chapter9 String的用法

    1)Which code fragment would correctly identify the number of arguments passed via the command line t ...

  2. Java题库——Chapter13抽象类和接口

    )What is the output of running class Test? public class Test { public static void main(String[ ] arg ...

  3. Java题库——Chapter8 对象和类

    1)________ represents an entity(实体) in the real world that can be distinctly identified. 1) _______ ...

  4. Java题库——Chapter16 JavaFX UI组件和多媒体

    Chapter 16 JavaFX UI Controls and Multimedia Section 16.2 Labeled and Label1. To create a label with ...

  5. Java题库——Chapter14 JavaFX基础

    Chapter 14 JavaFX Basics Section 14.2 JavaFX vs Swing and AWT1. Why is JavaFX preferred?a. JavaFX is ...

  6. Java题库——Chapter12 异常处理和文本IO

    异常处理 1)What is displayed on the console when running the following program? class Test { public stat ...

  7. Java题库——Chapter11 继承和多态

    1)Analyze the following code: public class Test { public static void main(String[ ] args) { B b = ne ...

  8. Java题库——Chapter10 面向对象思考

    1)You can declare two variables with the same name in ________. 1) _______ A)a method one as a forma ...

  9. Java题库——Chapter6 一维数组

    1)What is the representation of the third element in an array called a? 1) _______ A)a(3) B) a(2) C) ...

随机推荐

  1. ES6扩展运算符...

    对象的扩展运算符理解对象的扩展运算符其实很简单,只要记住一句话就可以: 对象中的扩展运算符(...)用于取出参数对象中的所有可遍历属性,拷贝到当前对象之中 let bar = { a: 1, b: 2 ...

  2. 我的第一个python web 开发框架

    1:数据库结构设计与创建 小白做好前端html设计后,马上开始进入数据库结构设计步骤. 在开始之前,小白回忆了一下老大在公司里培训时讲过的数据库设计解说: 对于初学者来说,很多拿到原型时不知道怎么设计 ...

  3. 【Android - 控件】之可悬浮列表StickyHeadersRecyclerView

    这是timehop的GitHub上发表的一个控件框架,大家可以去参考它的[GitHub]. 这里先贴出GitHub上提供的效果图: 要使用这个框架,我们需要首先导入它的依赖: compile 'com ...

  4. djanao请求生命周期

    djanao请求生命周期 浏览器发送请求到服务端 服务端的wsgi服务器接收到来自浏览器的请求, 对request做一些预处理, 把浏览器的请求信息(请求方式, 请求头, socket信息等)都封装在 ...

  5. DNS服务反向解析实验

    DNS域名解析服务是用于解析域名与ip地址对应关系的服务,功能上可以实现正向解析和反向解析 正向解析:根据主机名(域名)查找对应的IP地址. 反向解析:根据IP地址查找对应的主机名(域名). 下面我来 ...

  6. 简单易学的机器学习算法——决策树之ID3算法

    一.决策树分类算法概述     决策树算法是从数据的属性(或者特征)出发,以属性作为基础,划分不同的类.例如对于如下数据集 (数据集) 其中,第一列和第二列为属性(特征),最后一列为类别标签,1表示是 ...

  7. WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.

    利用vSphere调整各台虚拟机后,重新启动mesos,让其启动docker,并为每个container分配cpu和mem,但每次都有一个TASK_LOST. 查看mesos slave的log,发现 ...

  8. 深入理解 BigDecimal

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  9. SmartSVN提示 svn: File has inconsistent newlines 解决

    用SmartSVN提交代码的时候提示:svn: File has inconsistent newlines 本文转自:http://www.youduoshao.com/2014-10-05/201 ...

  10. iOS库的种类

    一.什么是库? 库是共享程序代码的方式,一般分为静态库和动态库. 二.静态库与动态库的区别? 静态库:链接时完整地拷贝至可执行文件中,被多次使用就有多份冗余拷贝. 动态库:链接时不复制,程序运行时由系 ...