Which interface represents a single row of data retrieved from a database in JDBC?
Answer: B
ResultSet interface represents the result set of a query. It contains the data returned from a database query and provides methods to access and traverse through the rows.
Q.22Medium
What does the PreparedStatement interface provide that Statement does not?
Answer: D
PreparedStatement provides both parameterized queries (preventing SQL injection) and precompilation of SQL statements, resulting in better security and performance compared to Statement.
Q.23Easy
In JDBC, what is the purpose of the Class.forName() method?
Answer: A
Class.forName() is used to load the JDBC driver class dynamically and register it with the DriverManager. For example: Class.forName("com.mysql.cj.jdbc.Driver");
Q.24Medium
Which exception is thrown when a database operation violates a unique constraint in JDBC?
Answer: D
SQLIntegrityConstraintViolationException is a specific subclass of SQLException that is thrown when integrity constraint violations occur, such as unique constraint violations.
Q.25Medium
What is the correct way to use try-with-resources for JDBC operations?
Answer: A
The try-with-resources statement automatically closes resources implementing AutoCloseable. Since Connection implements AutoCloseable, it will be automatically closed.
Advertisement
Q.26Medium
Which method is used to retrieve metadata about the database in JDBC?
Answer: B
The getDatabaseMetaData() method is called on a Connection object to retrieve DatabaseMetaData information about the database, such as table names, column information, etc.
Q.27Medium
In JDBC, what does the ResultSet.absolute(5) method do?
Answer: B
The absolute(int row) method moves the cursor to the specified row number (1-based indexing). absolute(5) moves the cursor directly to row 5.
Q.28Hard
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.29Medium
Which JDBC feature allows multiple SQL statements to be sent to the database in a single round trip?
Answer: B
Batch processing allows multiple SQL statements to be grouped and sent together using addBatch() and executeBatch() methods, improving performance by reducing network overhead.
Q.30Medium
In JDBC, which concurrency type allows modifications to the ResultSet?
Answer: B
CONCUR_UPDATABLE is the ResultSet concurrency type that allows the ResultSet to be updated. CONCUR_READ_ONLY prevents modifications.
Q.31Hard
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.32Hard
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.33Hard
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.34Easy
What happens when you call rs.next() on a ResultSet after the last row?
Answer: B
The next() method returns false when there are no more rows available. It does not throw an exception or loop back; it simply indicates that there are no additional rows to traverse.
Q.35Hard
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.36Medium
Which JDBC method allows you to retrieve column information such as column name, type, and size?
Answer: A
ResultSetMetaData interface provides information about the columns returned in a ResultSet, such as getColumnName(), getColumnType(), getColumnCount(), etc. Obtained via rs.getMetaData().
Q.37Hard
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.38Easy
Which interface in JDBC is used to execute a single SQL statement and return a ResultSet?
Answer: A
Statement interface is used to execute simple SQL queries without parameters and return results as ResultSet.
Q.39Easy
What is the primary advantage of using PreparedStatement over Statement in JDBC?
Answer: A
PreparedStatement pre-compiles SQL and uses parameterized queries, preventing SQL injection and improving performance.
Q.40Easy
Which JDBC method is used to retrieve the number of rows affected by an INSERT, UPDATE, or DELETE statement?
Answer: B
executeUpdate() returns an int representing the number of rows affected by the SQL statement execution.