Java III - Chapter 17

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

A

17.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.

C

17.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.

C

17.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(); } } A. 2 bytes. B. 4 bytes. C. 6 bytes. D. 8 bytes. E. 10 bytes.

A, B, C, D

17.13 Which of the following statements are true? (Multiple Answers) 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.

A, B, C, D, E

17.14 Which of the following statements are true? (Multiple Answers) 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.

D

17.17 To create a file, you can use __________. A. FileOutputStream B. FileWriter C. RandomAccessFile D. All of the above

C

17.18 Which of the following is the legal mode for creating a new RandomAccessFile stream? A. "w" B. 'r' C. "rw" D. "rwx"

C

17.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.

A

17.20 With which I/O class can you append or update a file? A. RandomAccessFile() B. OutputStream() C. DataOutputStream() D. None of the above

B

17.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()

A, B, C, D

17.4 Which of the following statements are true? (Multiple Answers) 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.

C

17.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")

E

17.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.

B

17.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.

A, B, C, D, E

7.2 Which of the following statements are true? (Multiple Answers) 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.

D

7.7 Which type of exception occurs when creating a DataInputStream for a nonexistent file? A. FileNotExist B. FileNotExistException C. FileNotFound D. FileNotFoundException

B

7.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");

A, B, C, D, E

Which of the following statements are true? (Multiple Answers) 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.


Ensembles d'études connexes

Saunders Ch.64 - Foundations of Mental Health Nursing Questions w/ Rationale

View Set

WCS Fill in Blank: Light, Mirrors, and Lenses

View Set

International Finance Ch 10 Questions

View Set

Operating systems test study guide

View Set