Home Subjects Java Programming I/O Streams

Java Programming
I/O Streams

Java OOP, collections, multithreading

53 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–30 of 53
Topics in Java Programming
Q.21 Medium I/O Streams
In a multi-threaded application, what is a concern when sharing streams between threads?
A Memory leaks
B Data corruption due to concurrent access
C Increased CPU usage
D Automatic buffering issues
Correct Answer:  B. Data corruption due to concurrent access
EXPLANATION

Streams are not thread-safe. Concurrent access by multiple threads can lead to data corruption. Synchronization or separate streams per thread are needed.

Test
Q.22 Medium I/O Streams
What exception is thrown when attempting to deserialize an object with a different serialVersionUID?
A IOException
B InvalidClassException
C ClassNotFoundException
D StreamCorruptedException
Correct Answer:  B. InvalidClassException
EXPLANATION

InvalidClassException is thrown when serialVersionUID doesn't match during deserialization, indicating class incompatibility.

Test
Q.23 Medium I/O Streams
Consider a scenario where you need to read a line from a file. Which class would be most efficient?
A FileInputStream reading byte by byte
B BufferedReader with readLine() method
C DataInputStream with readUTF()
D FileReader reading character by character
Correct Answer:  B. BufferedReader with readLine() method
EXPLANATION

BufferedReader.readLine() is the most efficient way to read complete lines as it buffers input and provides a dedicated method for line reading.

Test
Q.24 Medium I/O Streams
What is the purpose of the mark() and reset() methods in buffered streams?
A To bookmark a position and return to it later
B To create a checkpoint in file processing
C To mark end of stream
D To reset the stream encoding
Correct Answer:  A. To bookmark a position and return to it later
EXPLANATION

mark() records the current position in the stream, and reset() returns to that marked position, useful for lookahead operations.

Test
Q.25 Medium I/O Streams
When combining multiple input sources into one logical stream, which class should be used?
A BufferedInputStream
B PushbackInputStream
C SequenceInputStream
D ConcatInputStream
Correct Answer:  C. SequenceInputStream
EXPLANATION

SequenceInputStream concatenates multiple input streams, reading from them sequentially as if they were a single stream.

Test
Q.26 Medium I/O Streams
In a file compression utility, which stream would you use for reading compressed data?
A BufferedInputStream
B GZIPInputStream
C DataInputStream
D FileInputStream
Correct Answer:  B. GZIPInputStream
EXPLANATION

GZIPInputStream decompresses GZIP-compressed data, allowing transparent reading of compressed files as normal streams.

Test
Q.27 Medium I/O Streams
What is the primary purpose of transient keyword in Java serialization?
A Make objects read-only
B Exclude specific fields from serialization
C Improve serialization performance
D Synchronize object access
Correct Answer:  B. Exclude specific fields from serialization
EXPLANATION

The transient keyword marks fields that should not be serialized, useful for excluding sensitive data or non-serializable objects.

Test
Q.28 Medium I/O Streams
For processing delimited text files with variable field counts, which class is most suitable?
A DataInputStream
B BufferedReader with custom parsing logic
C ObjectInputStream
D RandomAccessFile
Correct Answer:  B. BufferedReader with custom parsing logic
EXPLANATION

BufferedReader combined with custom parsing logic (using split or regex) handles variable-length fields better than fixed-format alternatives.

Test
Q.29 Medium I/O Streams
Which method of PushbackInputStream allows you to return bytes to the stream to be read again?
A reset()
B unread()
C rewind()
D goBack()
Correct Answer:  B. unread()
EXPLANATION

The unread() method of PushbackInputStream allows you to push bytes back into the stream so they can be read again.

Test
Q.30 Medium I/O Streams
In a multi-threaded application, which stream class is thread-safe?
A FileInputStream
B BufferedReader
C PrintWriter
D FileOutputStream
Correct Answer:  C. PrintWriter
EXPLANATION

PrintWriter is synchronized, making it thread-safe. Other stream classes are not inherently thread-safe and require external synchronization in multi-threaded contexts.

Test
IGET
IGET AI
Online · Exam prep assistant
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