A batch update operation fails partially. How can you identify which statements failed in the batch?
Answer: B
executeBatch() returns an int array where each element represents the update count for the corresponding statement. A value of -3 (Statement.EXECUTE_FAILED) indicates failure for that statement.
Q.142Hard
In JDBC, what is the purpose of using a SavePoint in a transaction?
Answer: B
A SavePoint allows you to roll back part of a transaction without rolling back the entire transaction. You can create nested transactions by setting multiple savepoints and rolling back to specific ones.
Q.143Hard
A developer creates a JDBC connection but forgets to close it. What might be the consequence?
Answer: B
Unclosed connections remain allocated, eventually exhausting the connection pool and causing application failures.
Q.144Hard
Consider a scenario where a developer retrieves a ResultSet and then closes the Statement object. What happens to the ResultSet?
Answer: B
Closing a Statement typically invalidates its associated ResultSet in most JDBC drivers, though behavior may vary.
Q.145Hard
What is the optimal approach to handle JDBC resources to prevent memory leaks in a production application?
In a multi-threaded JDBC application, why should each thread have its own Connection object?
Answer: B
JDBC Connections are not thread-safe. Each thread must have its own Connection to prevent concurrent access issues and data corruption.
Q.147Hard
Consider a scenario: A developer uses Statement with user input directly in SQL queries. What is the primary risk?
Answer: B
Direct concatenation of user input in SQL queries exposes the application to SQL injection attacks. PreparedStatement should be used instead.
Q.148Hard
In a JDBC application, when using transactions with setAutoCommit(false), what happens to intermediate savepoints if rollback() is called?
Answer: C
When rollback() is called without savepoints, all changes made since the last commit are undone and rolled back.
Q.149Hard
Which JDBC feature should be used for executing multiple SQL statements in a batch for optimal performance?
Answer: B
executeBatch() with addBatch() reduces network round-trips and improves performance for bulk operations.
Q.150Hard
In JDBC, what is the significance of setting FetchSize on a Statement when dealing with large ResultSets?
Answer: A
FetchSize controls how many rows are fetched from the database at once, optimizing performance for large datasets.
Q.151Hard
When using JDBC with Spring Framework in 2024-25, which approach is recommended for resource management?
Answer: C
JdbcTemplate in Spring Framework automatically manages JDBC resources and is the recommended approach in modern applications.
Q.152Hard
A developer wants to retrieve data from a stored procedure that returns multiple result sets and output parameters. Which approach is correct?
Answer: C
CallableStatement is specifically designed for stored procedures, supporting registerOutParameter() for output parameters and getMoreResults() for multiple result sets.
Q.153Hard
A query execution takes 5 seconds consistently. Which JDBC optimization technique should NOT be used for this scenario?
Answer: D
If query execution itself is slow (5 seconds), the bottleneck is in database operations, not data transfer. Increasing setFetchSize() only optimizes data retrieval, not query execution.
Q.154Hard
In a JDBC application using batch updates, what happens if one statement in the batch fails?
Answer: D
executeBatch() behavior on failure varies by database and driver. BatchUpdateException is thrown, containing update counts for each statement. Explicit transaction control is recommended.
Q.155Hard
A Java application requires connection pooling to handle 1000+ concurrent database requests efficiently. Which JDBC component should be implemented?
Answer: B
DataSource with connection pooling frameworks (HikariCP, C3P0) efficiently manages connections by reusing them, reducing overhead. Direct getConnection() calls create new connections each time, which is inefficient for high concurrency.
Q.156Hard
In JDBC metadata operations, which method is used to retrieve information about table structure, column names, and their data types?
Answer: D
getMetaData() on ResultSet returns ResultSetMetaData which provides column information. getColumnCount() and getColumnName() are methods of ResultSetMetaData used to retrieve structure details.
Q.157Hard
Which of these correctly demonstrates the Producer Extends Consumer Super (PECS) principle?
Answer: A
PECS principle: use 'extends' when reading from a collection (source), use 'super' when writing to it (dest). Option A reads from source and writes to dest correctly.
Q.158Hard
What will happen with this code?
List list = new ArrayList<String>();
list.add(123); // Adding Integer
String s = (String) list.get(0);
Answer: A
Raw type List accepts any object. At runtime, the Integer 123 cannot be cast to String, causing ClassCastException.
Q.159Hard
Consider this code:
public <T extends Comparable<T>> T getMax(T a, T b) {
return a.compareTo(b) > 0 ? a : b;
}
What is the benefit of this recursive bound <T extends Comparable<T>>?
Answer: A
Recursive bound <T extends Comparable<T>> ensures that T implements Comparable interface specifically for comparing with its own type, providing type safety.
Q.160Hard
Which statement is true about generic inheritance?
Answer: B
ArrayList<Integer> is a subtype of List<Integer> because ArrayList is a subclass of List with the same type parameter.