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.2Hard
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.3Hard
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.4Hard
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.5Hard
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.
Advertisement
Q.6Hard
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.7Hard
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.8Hard
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.9Hard
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().
Q.10Hard
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.11Hard
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.12Hard
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.13Hard
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.14Hard
What is the optimal approach to handle JDBC resources to prevent memory leaks in a production application?