What happens if you try to read from a closed stream?
Answer: C
Attempting to read from a closed stream throws IOException. The stream should be checked and reopened if needed, or proper resource management (try-with-resources) should be used.
Q.582Medium
Which class wraps a byte stream to handle character encoding/decoding?
Answer: A
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.).
Q.583Medium
In the try-with-resources statement, what happens to resources automatically?
Answer: B
Try-with-resources automatically closes resources in reverse order (LIFO). All resources must implement AutoCloseable. This ensures proper resource cleanup even if exceptions occur.
Q.584Hard
What is the main advantage of using RandomAccessFile over sequential streams?
Answer: B
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.
Q.585Hard
When deserializing an object, which methods are called in order?
Answer: B
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.
Advertisement
Q.586Hard
Consider a scenario where you need to read a large binary file efficiently without loading it entirely into memory. Which approach combines best practices?
Answer: B
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.
Q.587Hard
What is the primary advantage of PushbackInputStream?
Answer: B
PushbackInputStream allows you to 'unread' bytes using the unread() method. This is useful for parsers that need to look ahead without consuming the data.
Q.588Easy
Which of the following classes is used to read primitive data types from an input stream in Java?
Answer: A
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.
Q.589Medium
In a file I/O operation, you need to write objects to a file and later retrieve them. Which approach is most appropriate?
Answer: B
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.
Q.590Medium
What will happen if you attempt to serialize a class that contains a non-serializable instance variable without declaring it as transient?
Answer: A
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.
Q.591Medium
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?
Answer: B
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.
Q.592Easy
Which interface must a class implement to be eligible for serialization in Java, and what is the significance of implementing it with no abstract methods?
Answer: B
Serializable is a marker interface (contains no methods) that signals to the JVM that objects of that class can be serialized. The absence of abstract methods means implementing classes don't need to override any methods; they just need to implement the interface to indicate serialization support.
Q.593Hard
In a multi-threaded application, multiple threads are writing to the same file simultaneously using FileOutputStream. What is the primary issue and best solution?
Answer: B
FileOutputStream is NOT thread-safe. When multiple threads write simultaneously, race conditions can cause data corruption or interleaved writes. Solutions include: synchronizing access using synchronized blocks, using thread-safe wrapper classes, or employing a single-threaded writer pattern with a queue. PrintWriter with autoFlush provides some buffering protection.
Q.594Easy
Which interface in JDBC is used to execute SQL queries and obtain results?
Answer: A
Statement interface is used to execute SQL queries. Connection is used to establish database connection, ResultSet holds query results, and Driver manages database connections.
Q.595Easy
What is the correct order of JDBC operations?
Answer: A
The correct JDBC sequence is: 1) Load the JDBC driver, 2) Create connection, 3) Create statement, 4) Execute query, 5) Process results, 6) Close resources.
Q.596Easy
Which JDBC driver type is platform-independent and does not require native code?
Answer: D
Type 4 (Thin) drivers are pure Java drivers, platform-independent, and don't require native code. They communicate directly with database using Java sockets.
Q.597Easy
What exception is thrown when a database connection cannot be established?
Answer: A
SQLException is thrown for database-related errors including connection failures. ClassNotFoundException occurs when JDBC driver class is not found.
Q.598Easy
Which method is used to retrieve a String value from a ResultSet object?
Answer: B
getString() method retrieves String values from ResultSet. getInt() gets integer values, and fetchString/readString are not valid JDBC methods.
Q.599Medium
What does the executeUpdate() method return in JDBC?
Answer: B
executeUpdate() returns an integer representing the number of rows affected by INSERT, UPDATE, or DELETE operations. executeQuery() returns ResultSet.
Q.600Medium
Which interface is used to execute precompiled SQL statements in JDBC?
Answer: B
PreparedStatement is used for precompiled SQL statements with parameters. It provides better performance and prevents SQL injection compared to Statement.