What is the correct way to ensure resources are properly closed in Java 7+?
Answer: B
Try-with-resources (try-with-resources statement) automatically closes AutoCloseable resources, ensuring proper cleanup even if exceptions occur.
Q.282Medium
What happens when you create a FileOutputStream with an existing file?
Answer: C
FileOutputStream(String filename) by default overwrites existing file. Use FileOutputStream(File, boolean append) with true to append.
Q.283Medium
What is the default buffer size used by BufferedInputStream?
Answer: C
The default buffer size for BufferedInputStream is 8192 bytes (8 KB). This can be overridden by using the constructor BufferedInputStream(InputStream, int size).
Q.284Medium
Which class is used to read primitive data types and strings from a binary stream?
Answer: B
DataInputStream provides methods like readInt(), readDouble(), readUTF() to read primitive data types and strings from binary streams.
Q.285Medium
Which of the following best describes the relationship between InputStream and Reader classes?
Answer: D
InputStream and Reader are independent class hierarchies. InputStream is for byte streams and Reader is for character streams. They do not extend each other.
Advertisement
Q.286Medium
In a scenario where you need to write formatted output (like printf), which class should be used?
Answer: C
PrintWriter provides print() and printf() methods for formatted output. It is the best choice for formatted writing to files or streams.
Q.287Medium
What is the difference between read() and read(byte[] b) methods in InputStream?
Answer: A
read() reads and returns a single byte as an int (0-255 or -1 for EOF). read(byte[] b) reads bytes into an array and returns the number of bytes read.
Q.288Medium
Which exception is thrown when you try to serialize an object that contains non-serializable fields?
Answer: B
NotSerializableException is thrown when attempting to serialize an object whose class does not implement Serializable or contains non-serializable fields.
Q.289Medium
You need to read a file line by line efficiently. Which approach is best?
Answer: B
BufferedReader with readLine() is the most efficient and convenient way to read files line by line. It provides buffering and a dedicated method for reading lines.
Q.290Medium
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.291Medium
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.292Medium
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.293Medium
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.294Medium
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.295Medium
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.296Medium
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.297Medium
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.
Q.298Medium
What is the primary advantage of using PreparedStatement over Statement?
Answer: A
PreparedStatement offers faster execution due to precompilation and prevents SQL injection through parameterized queries using placeholders (?).
Q.299Medium
Which method must be called to move cursor to the next row in ResultSet?
Answer: A
The next() method moves the ResultSet cursor to the next row and returns true if a row exists, false if no more rows. It's essential for iterating through results.
Q.300Medium
What is Connection pooling in JDBC?
Answer: A
Connection pooling maintains a pool of reusable database connections to improve performance and reduce overhead of creating new connections repeatedly.