Java Programming — I/O Streams
Java OOP, collections, multithreading
53 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 53 questions in I/O Streams
Q.1 Medium I/O Streams
Consider a scenario where you're processing a 5GB log file and need to count specific error messages. Which I/O strategy would be most memory-efficient?
A Load the entire file into a byte array and process it
B Use BufferedReader with readLine() in a loop to process line-by-line
C Use FileInputStream to read all bytes at once
D Convert the entire file to a String and use split() method
Correct Answer:  B. Use BufferedReader with readLine() in a loop to process line-by-line
EXPLANATION

BufferedReader with readLine() processes the file line-by-line, maintaining a constant memory footprint regardless of file size. Options A, C, and D would attempt to load the entire 5GB file into memory, causing OutOfMemoryError. This is the standard approach for large file processing.

Take Test
Q.2 Medium I/O Streams
What will happen if you attempt to serialize a class that contains a non-serializable instance variable without declaring it as transient?
A The program will compile successfully but throw NotSerializableException at runtime
B The compiler will generate a compile-time error
C Only the serializable fields will be written to the stream
D The non-serializable field will be automatically converted to String
Correct Answer:  A. The program will compile successfully but throw NotSerializableException at runtime
EXPLANATION

Java serialization requires all instance variables to be serializable or marked as transient. If a non-serializable object is encountered during serialization, NotSerializableException is thrown at runtime, not compile-time. There's no automatic conversion.

Take Test
Q.3 Medium I/O Streams
In a file I/O operation, you need to write objects to a file and later retrieve them. Which approach is most appropriate?
A Use FileOutputStream with DataOutputStream for object serialization
B Use ObjectOutputStream for serialization and ObjectInputStream for deserialization
C Use PrintWriter to write object toString() values
D Use ByteArrayOutputStream to store objects in memory
Correct Answer:  B. Use ObjectOutputStream for serialization and ObjectInputStream for deserialization
EXPLANATION

ObjectOutputStream and ObjectInputStream are specifically designed for object serialization and deserialization. They handle the complete object state preservation. Other options are either inefficient or unsuitable for preserving object state.

Take Test
Q.4 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.5 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
Advertisement
Q.6 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.7 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.8 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
Q.9 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.10 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
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