Java Programming — I/O Streams
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 31–40 of 100 questions in I/O Streams
Q.31 Medium I/O Streams
In Java NIO, which class is used for channel-based I/O?
A FileInputStream
B FileChannel
C BufferedInputStream
D DataInputStream
Correct Answer:  B. FileChannel
EXPLANATION

FileChannel provides non-blocking and efficient I/O operations, memory-mapped file access, and file locking capabilities compared to traditional streams.

Take Test
Q.32 Medium I/O Streams
What does the available() method in InputStream return?
A Number of bytes that can be read without blocking
B Total bytes in the stream
C Remaining bytes in the file
D Whether the stream is open
Correct Answer:  A. Number of bytes that can be read without blocking
EXPLANATION

available() returns an estimate of bytes that can be read without blocking. It doesn't return total file size.

Take Test
Q.33 Medium I/O Streams
Consider processing a 1GB file. Which approach would be most memory-efficient?
A Read entire file into a byte array
B Use BufferedInputStream with small buffer size
C Use BufferedInputStream with appropriate buffer size and process in chunks
D Use ObjectInputStream to load the whole file
Correct Answer:  C. Use BufferedInputStream with appropriate buffer size and process in chunks
EXPLANATION

Processing large files in chunks using BufferedInputStream with an appropriate buffer size (like 8KB-64KB) balances memory usage and I/O efficiency.

Take Test
Q.34 Medium I/O Streams
What is the difference between InputStreamReader and FileReader?
A InputStreamReader accepts any InputStream; FileReader is specifically for files
B FileReader is faster than InputStreamReader
C InputStreamReader supports custom character encoding; FileReader doesn't
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

InputStreamReader is a bridge from byte streams to character streams and accepts any InputStream with optional charset. FileReader is specifically for file input with default charset.

Take Test
Q.35 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.

Take Test
Advertisement
Q.36 Easy I/O Streams
Which stream class is used to write primitive data types in binary format?
A PrintWriter
B DataOutputStream
C BufferedOutputStream
D ObjectOutputStream
Correct Answer:  B. DataOutputStream
EXPLANATION

DataOutputStream provides methods like writeInt(), writeFloat(), writeDouble() to write primitive types in binary format.

Take Test
Q.37 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.

Take Test
Q.38 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.

Take Test
Q.39 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.

Take Test
Q.40 Easy I/O Streams
Which class provides buffering capability to improve I/O performance?
A FileInputStream
B BufferedInputStream
C ByteArrayInputStream
D PipedInputStream
Correct Answer:  B. BufferedInputStream
EXPLANATION

BufferedInputStream wraps another InputStream and buffers input, reducing the number of actual read operations and improving performance significantly.

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