Java Programming — I/O Streams
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 11–20 of 100 questions in I/O Streams
Q.11 Medium I/O Streams
In the try-with-resources statement, what happens to resources automatically?
A They are flushed but not closed
B They are closed in reverse order of opening
C They are closed in the order they were opened
D They must be manually closed in finally block
Correct Answer:  B. They are closed in reverse order of opening
EXPLANATION

Try-with-resources automatically closes resources in reverse order (LIFO). All resources must implement AutoCloseable. This ensures proper resource cleanup even if exceptions occur.

Take Test
Q.12 Medium I/O Streams
Which class wraps a byte stream to handle character encoding/decoding?
A InputStreamReader
B BufferedReader
C FileReader
D CharArrayReader
Correct Answer:  A. InputStreamReader
EXPLANATION

InputStreamReader is a bridge between byte streams and character streams. It converts bytes to characters using specified character encoding (UTF-8, ISO-8859-1, etc.).

Take Test
Q.13 Medium I/O Streams
What happens if you try to read from a closed stream?
A It returns -1
B It returns null
C It throws IOException
D It silently fails
Correct Answer:  C. It throws IOException
EXPLANATION

Attempting to read from a closed stream throws IOException. The stream should be checked and reopened if needed, or proper resource management (try-with-resources) should be used.

Take Test
Q.14 Medium I/O Streams
You need to read a file line by line efficiently. Which approach is best?
A FileInputStream with byte-by-byte reading
B BufferedReader with readLine()
C FileReader with manual character buffering
D Scanner with default buffer
Correct Answer:  B. BufferedReader with readLine()
EXPLANATION

BufferedReader with readLine() is the most efficient and convenient way to read files line by line. It provides buffering and a dedicated method for reading lines.

Take Test
Q.15 Medium I/O Streams
What is the purpose of the serialVersionUID field in a serializable class?
A To identify the version of the JVM
B To ensure version compatibility during deserialization
C To encrypt the serialized data
D To improve serialization speed
Correct Answer:  B. To ensure version compatibility during deserialization
EXPLANATION

serialVersionUID is used to verify that sender and receiver have compatible versions of a Serializable class. If serialVersionUID doesn't match during deserialization, InvalidClassException is thrown.

Take Test
Advertisement
Q.16 Medium I/O Streams
Which exception is thrown when you try to serialize an object that contains non-serializable fields?
A IOException
B NotSerializableException
C SerializationException
D InvalidObjectException
Correct Answer:  B. NotSerializableException
EXPLANATION

NotSerializableException is thrown when attempting to serialize an object whose class does not implement Serializable or contains non-serializable fields.

Take Test
Q.17 Medium I/O Streams
What is the difference between read() and read(byte[] b) methods in InputStream?
A read() returns a byte, read(byte[] b) returns multiple bytes in an array
B read(byte[] b) is faster than read()
C read() is for text files, read(byte[] b) is for binary files
D There is no practical difference
Correct Answer:  A. read() returns a byte, read(byte[] b) returns multiple bytes in an array
EXPLANATION

read() reads and returns a single byte as an int (0-255 or -1 for EOF). read(byte[] b) reads bytes into an array and returns the number of bytes read.

Take Test
Q.18 Medium I/O Streams
In a scenario where you need to write formatted output (like printf), which class should be used?
A BufferedWriter
B FileWriter
C PrintWriter
D OutputStreamWriter
Correct Answer:  C. PrintWriter
EXPLANATION

PrintWriter provides print() and printf() methods for formatted output. It is the best choice for formatted writing to files or streams.

Take Test
Q.19 Medium I/O Streams
Which of the following best describes the relationship between InputStream and Reader classes?
A Reader extends InputStream
B InputStream extends Reader
C Both extend a common abstract class
D They are independent, parallel hierarchies for byte and character streams
Correct Answer:  D. They are independent, parallel hierarchies for byte and character streams
EXPLANATION

InputStream and Reader are independent class hierarchies. InputStream is for byte streams and Reader is for character streams. They do not extend each other.

Take Test
Q.20 Easy I/O Streams
What will be the output of the following code?
FileOutputStream fos = new FileOutputStream("test.txt", true);
What does the 'true' parameter indicate?
A The file should be read-only
B The file should be overwritten
C The data should be appended to the existing file
D The file should be encrypted
Correct Answer:  C. The data should be appended to the existing file
EXPLANATION

The second parameter 'true' in FileOutputStream constructor indicates append mode. If 'false' or omitted, the file is overwritten. With 'true', new data is appended to the existing file.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips