Java Programming — I/O Streams
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 51–60 of 100 questions in I/O Streams
Q.51 Hard I/O Streams
In Java NIO, what is a key advantage of FileChannel over traditional I/O streams?
A It automatically handles encoding
B It supports memory mapping and direct buffer access
C It doesn't require closing
D It's easier to use than streams
Correct Answer:  B. It supports memory mapping and direct buffer access
EXPLANATION

FileChannel supports memory-mapped files and direct buffer manipulation, enabling high-performance I/O operations not possible with traditional streams.

Take Test
Q.52 Easy I/O Streams
What happens when you attempt to read from a closed InputStream?
A Returns -1
B Throws IOException
C Returns empty byte array
D Silently ignores the read request
Correct Answer:  B. Throws IOException
EXPLANATION

Attempting to read from a closed stream throws an IOException with message 'Stream closed', preventing accidental data processing from invalid sources.

Take Test
Q.53 Hard I/O Streams
For encrypting data while writing to a file, which approach is most appropriate?
A Use FileWriter directly with encoded strings
B Wrap FileOutputStream with CipherOutputStream
C Use PrintWriter with encryption library
D Convert data to hexadecimal before writing
Correct Answer:  B. Wrap FileOutputStream with CipherOutputStream
EXPLANATION

CipherOutputStream allows transparent encryption of data written through it, properly integrating with Java's cryptography framework.

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

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

Take Test
Q.56 Medium I/O Streams
What is the default charset used by InputStreamReader if none is specified?
A UTF-8
B ISO-8859-1
C System default charset
D ASCII
Correct Answer:  C. System default charset
EXPLANATION

If no charset is explicitly provided, InputStreamReader uses the platform's default charset, which varies by system configuration.

Take Test
Q.57 Medium I/O Streams
When processing a large XML file, which approach is most memory-efficient?
A Load entire file into memory using FileInputStream
B Use BufferedReader to read line-by-line
C Use SAX parser or streaming XML parser
D Convert file to ObjectInputStream
Correct Answer:  C. Use SAX parser or streaming XML parser
EXPLANATION

SAX parsers and streaming XML parsers process XML sequentially without loading the entire document, making them ideal for large files.

Take Test
Q.58 Easy I/O Streams
Which class would you use to read and write at arbitrary positions in a file?
A BufferedReader
B FileInputStream
C RandomAccessFile
D PushbackInputStream
Correct Answer:  C. RandomAccessFile
EXPLANATION

RandomAccessFile allows reading and writing at arbitrary positions using seek() method, useful for direct access to file contents.

Take Test
Q.59 Medium I/O Streams
In a real-time application processing sensor data streams, which buffering approach would minimize latency?
A Increase buffer size to 64KB
B Use unbuffered I/O or very small buffer sizes with frequent flush()
C Use ObjectInputStream for all data
D Implement custom byte arrays
Correct Answer:  B. Use unbuffered I/O or very small buffer sizes with frequent flush()
EXPLANATION

For real-time applications requiring minimal latency, smaller buffer sizes and frequent flushing ensure data is processed immediately rather than waiting for buffer to fill.

Take Test
Q.60 Medium I/O Streams
What exception is thrown when trying to deserialize an object that was serialized with a different serialVersionUID?
A ClassCastException
B InvalidClassException
C StreamCorruptedException
D NotSerializableException
Correct Answer:  B. InvalidClassException
EXPLANATION

InvalidClassException is thrown when a serialized object's serialVersionUID doesn't match the current class definition, indicating an incompatible class version.

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