In Java NIO, what is a key advantage of FileChannel over traditional I/O streams?
Answer: B
FileChannel supports memory-mapped files and direct buffer manipulation, enabling high-performance I/O operations not possible with traditional streams.
Q.122Hard
Which serialization approach maintains backward compatibility better?
Answer: C
Maintaining a constant serialVersionUID and implementing version-aware readObject() methods allows deserializing objects from different class versions.
Q.123Hard
For developing a high-performance file copying utility handling files up to 10GB, which approach is optimal?
Answer: C
FileChannel.transferTo() uses OS-level optimizations for file copying, and NIO channels with direct buffers are most efficient for large file transfers.
Q.124Hard
Given the code: PrintWriter pw = new PrintWriter(new FileWriter("file.txt")); What issue might occur?
Answer: B
PrintWriter uses buffering by default. If flush() or close() is not called, buffered data may not be written to disk.
Q.125Hard
Which approach would you use to read and write objects to a file efficiently in a production application?
Answer: B
ObjectInputStream/ObjectOutputStream with serialVersionUID ensures compatibility, versioning, and is the standard approach for object persistence in Java.
Advertisement
Q.126Hard
In a log aggregation system, what is the primary advantage of using RandomAccessFile over sequential streams?
Answer: B
RandomAccessFile supports seek() operation to jump to specific file positions, useful for reading log tail or accessing specific records directly.
Q.127Hard
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.128Hard
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.
Q.129Hard
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.130Hard
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.131Hard
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.132Hard
Consider code: String query = "SELECT * FROM users WHERE id=" + userId; stmt.executeQuery(query); What security issue exists?
Answer: B
This is vulnerable to SQL injection as user input is directly concatenated. Using PreparedStatement with parameters prevents this: "SELECT * FROM users WHERE id=?"
Q.133Hard
How would you implement batch processing in JDBC for multiple INSERT operations?
Answer: A
addBatch() adds SQL command to batch, executeBatch() executes all batched commands at once, improving performance significantly for multiple operations.
Q.134Hard
What is the difference between execute(), executeQuery(), and executeUpdate()?
Answer: A
execute() returns boolean (true if ResultSet available), executeQuery() returns ResultSet for SELECT, executeUpdate() returns row count for INSERT/UPDATE/DELETE.
Q.135Hard
Which JDBC feature allows you to call stored procedures?
Answer: C
CallableStatement is used to invoke stored procedures and functions. It extends PreparedStatement and supports IN, OUT, and INOUT parameters.
Q.136Hard
In JDBC, what does the ResultSet.TYPE_SCROLL_INSENSITIVE constant represent?
Answer: B
TYPE_SCROLL_INSENSITIVE creates scrollable ResultSet (can move forward/backward) but changes made to database after ResultSet creation are not reflected.
Q.137Hard
What is the difference between update() and updateRow() methods in ResultSet?
Answer: A
update*() methods (like updateInt(), updateString()) modify individual column values in the current row, while updateRow() commits all these changes to the database.
Q.138Hard
A developer wants to prevent SQL injection attacks while executing dynamic queries. Which approach is most secure?
Answer: B
PreparedStatement with parameterized queries is the most secure approach as it separates SQL logic from data. The database treats parameters as data, not executable code, preventing SQL injection.
Q.139Hard
What is the default transaction isolation level in JDBC when autocommit is disabled?
Answer: D
The default isolation level depends on the specific database and JDBC driver configuration. Different databases have different defaults (MySQL is REPEATABLE_READ, Oracle is READ_COMMITTED).
Q.140Hard
Which method is used to check if a CallableStatement has a return value from a stored procedure?
Answer: B
The wasNull() method checks if the last value retrieved from a CallableStatement was NULL. To get the return value, you typically use getInt(), getString(), etc., and then check wasNull().