Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 211–220 of 476
Topics in Java Programming
Q.211 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
Q.212 Medium I/O Streams
A file contains 1000 lines. Using BufferedReader.readLine() repeatedly reads all lines. What is the time complexity?
A O(1)
B O(log n)
C O(n)
D O(n²)
Correct Answer:  C. O(n)
EXPLANATION

Reading each line requires processing each character once, resulting in O(n) complexity where n is total characters.

Take Test
Q.213 Medium I/O Streams
Which scenario would require using a SequenceInputStream?
A Reading from multiple files sequentially as a single stream
B Reading very large files
C Parallel reading from multiple files
D Converting stream encoding
Correct Answer:  A. Reading from multiple files sequentially as a single stream
EXPLANATION

SequenceInputStream concatenates multiple input streams, allowing them to be read as one continuous stream.

Take Test
Q.214 Medium I/O Streams
What happens when you write to a closed PrintWriter?
A The data is buffered and written on next open
B IOException is thrown
C Data is silently discarded
D The stream reopens automatically
Correct Answer:  C. Data is silently discarded
EXPLANATION

PrintWriter suppresses IOException and continues silently, unlike other streams.

Take Test
Q.215 Medium I/O Streams
What is the serialVersionUID used for in Java serialization?
A To store the Java version number
B To identify the class version for deserialization compatibility
C To encrypt the serialized object
D To track the number of times an object was serialized
Correct Answer:  B. To identify the class version for deserialization compatibility
EXPLANATION

serialVersionUID identifies class versions. Mismatched versions cause InvalidClassException during deserialization.

Take Test
Q.216 Medium I/O Streams
In a CSV file processing application, why is BufferedReader preferred over FileReader for reading large files?
A BufferedReader automatically parses CSV format
B BufferedReader reduces I/O operations by reading data in chunks
C FileReader cannot read text files
D BufferedReader is faster at character encoding
Correct Answer:  B. BufferedReader reduces I/O operations by reading data in chunks
EXPLANATION

BufferedReader buffers input, reducing system calls and I/O overhead, improving performance significantly.

Take Test
Q.217 Medium I/O Streams
When implementing Externalizable interface, which method is mandatory?
A serialize()
B writeObject() and readObject()
C writeExternal() and readExternal()
D write() and read()
Correct Answer:  C. writeExternal() and readExternal()
EXPLANATION

Externalizable requires implementation of writeExternal() and readExternal() for complete control over serialization.

Take Test
Q.218 Medium I/O Streams
Which statement about ObjectOutputStream.reset() is correct?
A It resets the file pointer to the beginning
B It clears the internal object graph cache to enable re-serialization of identical objects
C It closes the stream and reopens it
D It deletes the serialized file
Correct Answer:  B. It clears the internal object graph cache to enable re-serialization of identical objects
EXPLANATION

reset() clears ObjectOutputStream's internal cache of serialized objects, allowing duplicate objects to be serialized again.

Take Test
Q.219 Medium I/O Streams
A serialized object contains a field marked as 'transient'. What happens to this field when the object is deserialized?
A It retains its original value from before serialization
B It is set to its default value (null for objects, 0 for primitives)
C An exception is thrown during deserialization
D It becomes immutable
Correct Answer:  B. It is set to its default value (null for objects, 0 for primitives)
EXPLANATION

Transient fields are excluded from serialization and are initialized to default values upon deserialization.

Take Test
Q.220 Medium I/O Streams
A Java program needs to read a file containing 10 million integers. Which approach would be most memory-efficient?
A Use FileInputStream with a byte array buffer
B Load entire file into memory using Scanner
C Use DataInputStream with buffering for sequential reading
D Read all bytes at once using Files.readAllBytes()
Correct Answer:  C. Use DataInputStream with buffering for sequential reading
EXPLANATION

DataInputStream with buffering allows sequential reading without loading the entire file into memory, making it ideal for large files.

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