Java Programming — I/O Streams
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 100 questions in I/O Streams
Q.1 Hard I/O Streams
In a multi-threaded application, multiple threads are writing to the same file simultaneously using FileOutputStream. What is the primary issue and best solution?
A FileOutputStream is thread-safe by default, no issues will occur
B Data corruption can occur; use synchronized blocks or PrintWriter with autoFlush enabled
C Only the last thread's data will be written; use multiple FileOutputStreams
D FileOutputStream will throw ConcurrentModificationException automatically
Correct Answer:  B. Data corruption can occur; use synchronized blocks or PrintWriter with autoFlush enabled
EXPLANATION

FileOutputStream is NOT thread-safe. When multiple threads write simultaneously, race conditions can cause data corruption or interleaved writes. Solutions include: synchronizing access using synchronized blocks, using thread-safe wrapper classes, or employing a single-threaded writer pattern with a queue. PrintWriter with autoFlush provides some buffering protection.

Take Test
Q.2 Easy I/O Streams
Which interface must a class implement to be eligible for serialization in Java, and what is the significance of implementing it with no abstract methods?
A Cloneable interface; it marks the class as eligible for deep copying
B Serializable interface; it's a marker interface with no abstract methods that indicates serialization capability
C Externalizable interface; it provides default serialization behavior
D Comparable interface; it enables object comparison during serialization
Correct Answer:  B. Serializable interface; it's a marker interface with no abstract methods that indicates serialization capability
EXPLANATION

Serializable is a marker interface (contains no methods) that signals to the JVM that objects of that class can be serialized. The absence of abstract methods means implementing classes don't need to override any methods; they just need to implement the interface to indicate serialization support.

Take Test
Q.3 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.4 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.5 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
Advertisement
Q.6 Easy I/O Streams
Which of the following classes is used to read primitive data types from an input stream in Java?
A DataInputStream
B BufferedInputStream
C FileInputStream
D ObjectInputStream
Correct Answer:  A. DataInputStream
EXPLANATION

DataInputStream is specifically designed to read primitive data types like int, double, boolean, etc. from an input stream. BufferedInputStream provides buffering, FileInputStream reads from files, and ObjectInputStream deserializes objects.

Take Test
Q.7 Hard I/O Streams
What is the primary advantage of PushbackInputStream?
A It reads data faster than regular InputStream
B It allows pushing back bytes to be read again
C It automatically compresses data
D It provides thread-safe reading
Correct Answer:  B. It allows pushing back bytes to be read again
EXPLANATION

PushbackInputStream allows you to 'unread' bytes using the unread() method. This is useful for parsers that need to look ahead without consuming the data.

Take Test
Q.8 Hard I/O Streams
Consider a scenario where you need to read a large binary file efficiently without loading it entirely into memory. Which approach combines best practices?
A FileInputStream with small fixed-size buffer array
B BufferedInputStream with DataInputStream for typed data
C FileInputStream with manual buffering
D ByteArrayInputStream after reading entire file
Correct Answer:  B. BufferedInputStream with DataInputStream for typed data
EXPLANATION

BufferedInputStream provides automatic buffering (8KB default), and DataInputStream allows reading typed data. Together they enable efficient reading of large binary files without loading everything into memory.

Take Test
Q.9 Hard I/O Streams
When deserializing an object, which methods are called in order?
A Constructor, then readObject()
B readObject() only, constructor is not called
C readObject(), then constructor
D Depends on the class implementation
Correct Answer:  B. readObject() only, constructor is not called
EXPLANATION

During deserialization, the constructor is NOT called. readObject() restores the object state directly from the serialized data. This is why transient fields need special handling.

Take Test
Q.10 Hard I/O Streams
What is the main advantage of using RandomAccessFile over sequential streams?
A Faster reading speed
B Ability to seek to any position in the file
C Smaller memory footprint
D Better thread safety
Correct Answer:  B. Ability to seek to any position in the file
EXPLANATION

RandomAccessFile allows you to read/write at any position in a file using seek() method. Sequential streams can only read/write sequentially from current position.

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