Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 391–400 of 958
Topics in Java Programming
Q.391 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.392 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.393 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
Q.394 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.395 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.396 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.397 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.398 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
Q.399 Easy I/O Streams
What does the flush() method do in output streams?
A Closes the stream
B Clears the buffer and writes data to the underlying stream
C Resets the stream position to the beginning
D Marks the stream for garbage collection
Correct Answer:  B. Clears the buffer and writes data to the underlying stream
EXPLANATION

flush() forces any buffered data to be written to the underlying stream immediately, ensuring data is not lost in the buffer.

Take Test
Q.400 Easy I/O Streams
Which interface must be implemented by a class to make its objects serializable?
A Cloneable
B Serializable
C Comparable
D Iterable
Correct Answer:  B. Serializable
EXPLANATION

The Serializable interface is a marker interface that indicates a class can be serialized. It's found in java.io package and requires no methods to be implemented.

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