
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































CS 1302: Introduction to Java Programming Prep with Detailed Rationales Course Code: CS 1302 Course Name: Introduction to Java Programming Topic: Binary I/O & Serialization Academic Year: 2026/
📋 STUDY CORE TOPICS OUTLINE
📚 TEXTBOOK ALIGNMENT INDEX
SectionModule FocusQuestion Range Part 1Core Byte Streams & Binary I/O FundamentalsQuestions 1–30| Part 2 | Filter Streams, Data Formatting & Buffering | Questions 31–60 | | Part 3 | Object Serialization & Transient Field Management | Questions 61–90 | | Part 4 | Random Access Files & Direct Pointer Manipulation | Questions 91–120 | | Part 5 | Advanced Serialization, Externalizable & Complex Streams | Questions 121–
Part 1: Core Byte Streams & Binary I/O Fundamentals
Comparing Text I/O and Binary I/O in Java, which statement correctly identifies how raw byte data is written to a file using Binary I/O? A. Text I/O encodes characters using system default encoding, while Binary I/O writes exact byte values directly to the output destination without encoding conversion. B. Binary I/O translates characters into human-readable Unicode text before flushing the buffer. C. Text I/O processes raw bytes directly without using character sets or character conversion tables. D. Binary I/O automatically attaches a text header to every file created on the disk. CORRECT ANSWER: A. Text I/O encodes characters using system default encoding, while Binary I/O writes exact byte values directly to the output destination without encoding conversion. RATIONALE: Text I/O performs automatic character encoding (e.g., converting Java's internal 16-bit Unicode char to UTF-8 or ASCII text bytes). Binary I/O bypasses character encoding entirely, writing the exact raw bytes residing in memory directly to the storage medium. This makes binary I/O significantly faster and more storage-efficient for non-text data.
What value does the read() method of InputStream return when it reaches the end of file (EOF)? A. 0 B. -1
RATIONALE: The FileOutputStream(String name, boolean append) constructor takes a boolean second parameter. Setting this value to true configures the stream to append newly written bytes to the end of the file rather than clearing existing content.
Which abstract class serves as the root superclass for all binary input streams in Java? A. java.io.Reader B. java.io.InputStream C. java.io.DataInput D. java.io.ObjectInput CORRECT ANSWER: B. java.io.InputStream RATIONALE: java.io.InputStream is the base abstract superclass for all byte- oriented input streams in Java. Reader is the base abstract superclass for character- oriented input streams, while DataInput and ObjectInput are interfaces.
What is the primary technical reason to prefer Binary I/O over Text I/O for non-textual application data? A. Binary I/O files can easily be edited in text editors such as Notepad. B. Binary I/O avoids encoding/decoding overhead and preserves exact binary values without loss or string conversion overhead. C. Text I/O takes up significantly less disk space for numerical data values. D. Binary I/O handles line-break differences across operating systems automatically. CORRECT ANSWER: B. Binary I/O avoids encoding/decoding overhead and preserves exact binary values without loss or string conversion overhead. RATIONALE: Binary I/O preserves exact bit patterns of memory structures. Writing a 64-bit double value in text mode requires converting it into a string of ASCII/Unicode digit characters (which can take 10–20 bytes depending on formatting). Binary I/O writes exactly 8 raw bytes, which improves execution efficiency and saves storage.
Which method of FileInputStream reads a sequence of bytes into a byte array starting at a specific offset? A. read(byte[] b) B. read(byte[] b, int off, int len) C. readBuffer(byte[] b, int start, int end) D. readBytes(byte[] b, int off, int len) CORRECT ANSWER: B. read(byte[] b, int off, int len) RATIONALE: The overload read(byte[] b, int off, int len) reads up to len bytes of data from the input stream into an array of bytes, storing the first read byte at b[off].
What happens if you attempt to create a FileInputStream for a file that does not exist? A. The file is created automatically. B. A FileNotFoundException is thrown at runtime. C. The stream opens with an empty file descriptor and returns 0 bytes. D. The JVM creates a temporary file in memory. CORRECT ANSWER: B. A FileNotFoundException is thrown at runtime. RATIONALE: Unlike FileOutputStream, FileInputStream requires an existing source file to read from. Attempting to open a non-existent file causes Java to throw a FileNotFoundException.
Which statement accurately describes the behavior of the close() method on Java binary stream objects? A. Closing a stream is optional because the Garbage Collector always handles system resources immediately.
RATIONALE: FileOutputStream is an output stream designed for writing raw byte data to a file. FileWriter, PrintWriter, and BufferedWriter are character stream components.
Which interface must a class implement if it wants to be processed by a try-with- resources block to ensure automatic resource closure? A. java.io.Serializable B. java.lang.AutoCloseable C. java.io.Flushable D. java.lang.Cloneable CORRECT ANSWER: B. java.lang.AutoCloseable RATIONALE: The try-with-resources statement automatically manages resources that implement java.lang.AutoCloseable (or its subinterface java.io.Closeable).
What is the default return type of the fundamental InputStream.read() method? A. byte B. short C. int D. char CORRECT ANSWER: C. int RATIONALE: InputStream.read() returns an int rather than a byte so it can represent all valid unsigned byte values (0 to 255) while reserving -1 as an end-of- file signal.
Which of the following is NOT a standard abstract base class in the java.io package?
A. InputStream B. OutputStream C. DataStream D. Reader CORRECT ANSWER: C. DataStream RATIONALE: Java provides InputStream, OutputStream, Reader, and Writer as standard base stream classes. There is no base class named DataStream; concrete filter stream implementations are named DataInputStream and DataOutputStream.
What is the effect of invoking flush() on a FileOutputStream? A. It empties the source file of all data. B. It forces any buffered output bytes to be written down to the underlying disk hardware. C. It resets the stream position back to byte offset 0. D. It closes the file output stream permanently. CORRECT ANSWER: B. It forces any buffered output bytes to be written down to the underlying disk hardware. RATIONALE: Operating systems and Java streams often cache byte writes in memory buffers for performance. Calling flush() forces all cached bytes out of the stream buffer and onto the underlying storage system.
When reading a file using FileInputStream in = new FileInputStream("image.png");, what happens if the file exists but read permissions are denied by the operating system? A. read() returns -1 immediately. B. A FileNotFoundException (or SecurityException) is thrown upon stream instantiation. C. The system bypasses permissions and reads default 0-bytes.
RATIONALE: Option A creates the stream inside the try (...) control parenthesis. This ensures that close() is automatically executed when the block finishes, while the while loop checks for the end-of-file condition (-1).
How many bytes are consumed when writing a single char value using a standard text stream in UTF-16 mode compared to raw binary byte stream operations? A. Binary streams write 1 byte per raw value; UTF-16 text streams write 2 bytes per standard character. B. Binary streams write 4 bytes for characters; UTF-16 writes 1 byte. C. Both write 8 bytes per character regardless of encoding settings. D. UTF-16 uses 4 bytes per character, while binary streams always compress to 0. bytes. CORRECT ANSWER: A. Binary streams write 1 byte per raw value; UTF-16 text streams write 2 bytes per standard character. RATIONALE: Java char variables occupy 16 bits (2 bytes). Text streams converting to UTF-16 output 2 bytes per character, whereas a raw byte stream (OutputStream) writes raw single bytes (8 bits) at a time.
Which class is best suited for copying a non-text binary file (e.g., an MP3 audio file or JPEG image) from one disk location to another? A. FileReader / FileWriter B. FileInputStream / FileOutputStream C. BufferedReader / BufferedWriter D. Scanner / PrintWriter CORRECT ANSWER: B. FileInputStream / FileOutputStream RATIONALE: Media files contain raw binary bytes. Character-oriented classes (FileReader, FileWriter, Scanner, PrintWriter) modify character representations based on character sets, which corrupts binary executable and media files. Byte streams (FileInputStream/FileOutputStream) copy exact raw binary data without modification.
What happens if you execute FileOutputStream output = new FileOutputStream("file.dat"); when "file.dat" already exists with data? A. The file is appended to automatically. B. The existing contents of "file.dat" are erased (truncated to 0 bytes). C. An IOException is thrown immediately. D. A duplicate file named "file(1).dat" is created. CORRECT ANSWER: B. The existing contents of "file.dat" are erased (truncated to 0 bytes). RATIONALE: Calling the single-argument FileOutputStream(String name) constructor overwrites any existing target file by truncating its contents to 0 bytes unless the append parameter is explicitly specified as true.
Which method skips over and discards n bytes of data from an input stream? A. discard(long n) B. skip(long n) C. jump(long n) D. seek(long n) CORRECT ANSWER: B. skip(long n) RATIONALE: InputStream.skip(long n) attempts to skip over and discard n bytes of data from the input stream and returns the actual number of bytes skipped.
What is the key difference between InputStream and Reader in Java I/O architecture? A. InputStream processes 8-bit raw bytes; Reader processes 16-bit Unicode characters.
RATIONALE: InputStream.markSupported() returns a boolean indicating whether the specific stream implementation supports mark() and reset() operations. (Standard FileInputStream returns false).
Why is it recommended to buffer raw binary file streams using BufferedInputStream? A. Buffering forces the system to automatically compress file contents on disk. B. Buffering reduces physical disk access operations by reading large blocks of bytes into memory at once. C. Buffering prevents binary corruption during byte conversion. D. Buffering converts byte streams into object streams automatically. CORRECT ANSWER: B. Buffering reduces physical disk access operations by reading large blocks of bytes into memory at once. RATIONALE: Reading bytes one at a time directly from storage requires disk system calls, which creates performance overhead. BufferedInputStream reads blocks of data (e.g., 8 KB) into an in-memory buffer, speeding up read operations.
What exception is thrown when an input stream encounters an unintended or premature end of file while reading binary structured data? A. NullPointerException B. FileNotFoundException C. EOFException D. BufferUnderflowException CORRECT ANSWER: C. EOFException RATIONALE: Subclasses like DataInputStream throw EOFException (which extends IOException) when attempting to read binary data structures (e.g., readInt(), readDouble()) if the end of the stream is reached before reading all required bytes.
Which byte sequence represents the first 2 bytes (magic number) of a standard compiled Java .class byte-code file? A. 0xCA 0xFE B. 0xFF 0xD8 C. 0x89 0x50 D. 0x47 0x49 CORRECT ANSWER: A. 0xCA 0xFE RATIONALE: Java .class files begin with the hexadecimal byte signature 0xCAFE BABE. This header identifies the file as executable Java bytecode when read by binary input streams.
If an application attempts to write a byte array to an OutputStream that was closed, what exception is raised? A. IllegalStateException B. IOException C. StreamClosedException D. NullPointerException CORRECT ANSWER: B. IOException RATIONALE: Stream operations invoked on a closed stream throw an IOException (or one of its specific subclasses like SocketException).
Which parameter configuration correctly creates a FileOutputStream that writes to a file relative to the project root directory? A. new FileOutputStream("src/data/out.dat") B. new FileOutputStream("C:\\root\\data\\out.dat") C. new FileOutputStream("/system/data/out.dat")
FileInputStream) to dynamically extend their functionality (adding structured primitive reading or memory buffering) without altering the underlying target stream directly.
What is the result of using DataInputStream.readUTF() to read data from a file written with DataOutputStream.writeUTF("Hello")? A. It returns the original String "Hello". B. It returns a byte array containing ASCII bytes. C. It throws an EncodingException. D. It returns a char[] array containing UTF-16 code units. CORRECT ANSWER: A. It returns the original String "Hello". RATIONALE: writeUTF() writes a string using a modified UTF-8 encoding prefixed by a 2-byte length header. readUTF() reads this binary header, reads the encoded character bytes, and reconstructs the original String object.
How many bytes does DataOutputStream.writeDouble(3.14159) write to the underlying stream? A. 4 bytes B. 8 bytes C. 16 bytes D. Dynamic length based on digit count CORRECT ANSWER: B. 8 bytes RATIONALE: A Java double is a 64-bit IEEE 754 floating-point value. DataOutputStream.writeDouble() always writes exactly 8 raw binary bytes to the stream.
If you attempt to read a value using DataInputStream.readInt() , but the binary file was written using DataOutputStream.writeLong() , what will happen? A. Java converts the long value to an int automatically. B. The read operation completes without error, but the retrieved integer value will be corrupted because it reads only the first 4 bytes of the 8-byte long. C. A TypeMismatchException is thrown immediately. D. The system resets stream position to byte offset zero. CORRECT ANSWER: B. The read operation completes without error, but the retrieved integer value will be corrupted because it reads only the first 4 bytes of the 8- byte long. RATIONALE: DataInputStream does not store type metadata with primitive values. It simply reads fixed byte counts (readInt() reads 4 bytes; readLong() reads 8 bytes). Reading data with a different method than the one used to write it results in byte misalignment and corrupted values.
Which method of DataInputStream reads a single binary boolean value from an underlying input stream? A. readBoolean() B. readBool() C. readBit() D. readByteAsBoolean() CORRECT ANSWER: A. readBoolean() RATIONALE: DataInputStream.readBoolean() reads 1 byte from the underlying input stream: a value of 0 returns false, while any non-zero value returns true.
What is the primary advantage of wrapping a FileInputStream inside a BufferedInputStream? A. It allows direct serialization of complex domain objects.
C. EOFException D. FileNotFoundException CORRECT ANSWER: C. EOFException RATIONALE: If a method like readInt() attempts to read 4 bytes but fewer than 4 bytes remain in the stream before hitting the end of the file, DataInputStream throws an EOFException.
Which DataOutputStream method writes a string as a sequence of 2-byte character values? A. writeChars(String s) B. writeBytes(String s) C. writeUTF(String s) D. writeString(String s) CORRECT ANSWER: A. writeChars(String s) RATIONALE: writeChars(String s) writes every character in the string to the output stream as a 2 - byte sequence (16 bits per character).
What is the byte length prefix size stored by DataOutputStream.writeUTF() at the start of the written encoded string? A. 1 byte B. 2 bytes C. 4 bytes D. 8 bytes CORRECT ANSWER: B. 2 bytes RATIONALE: writeUTF() writes a 2-byte unsigned integer indicating the length of the string in bytes, followed by the modified UTF-8 representation of the characters.
Because of this 2-byte length field, the maximum supported byte length for a string written with writeUTF() is 65,535 bytes.
How many total bytes are written to disk by executing dos.writeInt(100); dos.writeChar('A'); dos.writeDouble(5.5); on a DataOutputStream dos? A. 14 bytes B. 12 bytes C. 16 bytes D. 10 bytes CORRECT ANSWER: A. 14 bytes RATIONALE: An int occupies 4 bytes, a char occupies 2 bytes, and a double occupies 8 bytes. Calculating total written size: 4 + 2 + 8 = 14 bytes.
Which method in DataInputStream reads 2 signed bytes and converts them into a primitive integer type ranging from - 32,768 to 32,767? A. readShort() B. readUnsignedShort() C. readInt() D. readByte() CORRECT ANSWER: A. readShort() RATIONALE: readShort() reads 2 bytes from the input stream and returns a signed 16-bit short integer value.
What is the primary difference between DataOutputStream.writeBytes(String s) and DataOutputStream.writeChars(String s)?