Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 361–370 of 958
Topics in Java Programming
Q.361 Medium I/O Streams
Consider a scenario where you're processing a 5GB log file and need to count specific error messages. Which I/O strategy would be most memory-efficient?
A Load the entire file into a byte array and process it
B Use BufferedReader with readLine() in a loop to process line-by-line
C Use FileInputStream to read all bytes at once
D Convert the entire file to a String and use split() method
Correct Answer:  B. Use BufferedReader with readLine() in a loop to process line-by-line
EXPLANATION

BufferedReader with readLine() processes the file line-by-line, maintaining a constant memory footprint regardless of file size. Options A, C, and D would attempt to load the entire 5GB file into memory, causing OutOfMemoryError. This is the standard approach for large file processing.

Take Test
Q.362 Medium I/O Streams
What will happen if you attempt to serialize a class that contains a non-serializable instance variable without declaring it as transient?
A The program will compile successfully but throw NotSerializableException at runtime
B The compiler will generate a compile-time error
C Only the serializable fields will be written to the stream
D The non-serializable field will be automatically converted to String
Correct Answer:  A. The program will compile successfully but throw NotSerializableException at runtime
EXPLANATION

Java serialization requires all instance variables to be serializable or marked as transient. If a non-serializable object is encountered during serialization, NotSerializableException is thrown at runtime, not compile-time. There's no automatic conversion.

Take Test
Q.363 Medium I/O Streams
In a file I/O operation, you need to write objects to a file and later retrieve them. Which approach is most appropriate?
A Use FileOutputStream with DataOutputStream for object serialization
B Use ObjectOutputStream for serialization and ObjectInputStream for deserialization
C Use PrintWriter to write object toString() values
D Use ByteArrayOutputStream to store objects in memory
Correct Answer:  B. Use ObjectOutputStream for serialization and ObjectInputStream for deserialization
EXPLANATION

ObjectOutputStream and ObjectInputStream are specifically designed for object serialization and deserialization. They handle the complete object state preservation. Other options are either inefficient or unsuitable for preserving object state.

Take Test
Q.364 Easy I/O Streams
Which of the following classes is used to read primitive data types from an input stream in Java?
A DataInputStream
B BufferedInputStream
C FileInputStream
D ObjectInputStream
Correct Answer:  A. DataInputStream
EXPLANATION

DataInputStream is specifically designed to read primitive data types like int, double, boolean, etc. from an input stream. BufferedInputStream provides buffering, FileInputStream reads from files, and ObjectInputStream deserializes objects.

Take Test
Q.365 Hard I/O Streams
What is the primary advantage of PushbackInputStream?
A It reads data faster than regular InputStream
B It allows pushing back bytes to be read again
C It automatically compresses data
D It provides thread-safe reading
Correct Answer:  B. It allows pushing back bytes to be read again
EXPLANATION

PushbackInputStream allows you to 'unread' bytes using the unread() method. This is useful for parsers that need to look ahead without consuming the data.

Take Test
Q.366 Hard I/O Streams
Consider a scenario where you need to read a large binary file efficiently without loading it entirely into memory. Which approach combines best practices?
A FileInputStream with small fixed-size buffer array
B BufferedInputStream with DataInputStream for typed data
C FileInputStream with manual buffering
D ByteArrayInputStream after reading entire file
Correct Answer:  B. BufferedInputStream with DataInputStream for typed data
EXPLANATION

BufferedInputStream provides automatic buffering (8KB default), and DataInputStream allows reading typed data. Together they enable efficient reading of large binary files without loading everything into memory.

Take Test
Q.367 Hard I/O Streams
When deserializing an object, which methods are called in order?
A Constructor, then readObject()
B readObject() only, constructor is not called
C readObject(), then constructor
D Depends on the class implementation
Correct Answer:  B. readObject() only, constructor is not called
EXPLANATION

During deserialization, the constructor is NOT called. readObject() restores the object state directly from the serialized data. This is why transient fields need special handling.

Take Test
Q.368 Hard I/O Streams
What is the main advantage of using RandomAccessFile over sequential streams?
A Faster reading speed
B Ability to seek to any position in the file
C Smaller memory footprint
D Better thread safety
Correct Answer:  B. Ability to seek to any position in the file
EXPLANATION

RandomAccessFile allows you to read/write at any position in a file using seek() method. Sequential streams can only read/write sequentially from current position.

Take Test
Q.369 Medium I/O Streams
In the try-with-resources statement, what happens to resources automatically?
A They are flushed but not closed
B They are closed in reverse order of opening
C They are closed in the order they were opened
D They must be manually closed in finally block
Correct Answer:  B. They are closed in reverse order of opening
EXPLANATION

Try-with-resources automatically closes resources in reverse order (LIFO). All resources must implement AutoCloseable. This ensures proper resource cleanup even if exceptions occur.

Take Test
Q.370 Medium I/O Streams
Which class wraps a byte stream to handle character encoding/decoding?
A InputStreamReader
B BufferedReader
C FileReader
D CharArrayReader
Correct Answer:  A. InputStreamReader
EXPLANATION

InputStreamReader is a bridge between byte streams and character streams. It converts bytes to characters using specified character encoding (UTF-8, ISO-8859-1, etc.).

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