Showing 61–70 of 212 questions
When using JDBC with Spring Framework in 2024-25, which approach is recommended for resource management?
A
Manual try-catch blocks
B
Try-with-resources statement
C
JdbcTemplate with Spring
D
DriverManager only
Correct Answer:
C. JdbcTemplate with Spring
EXPLANATION
JdbcTemplate in Spring Framework automatically manages JDBC resources and is the recommended approach in modern applications.
In JDBC, what is the significance of setting FetchSize on a Statement when dealing with large ResultSets?
A
It determines the maximum number of rows to fetch from database in one round-trip
B
It sets the total memory allocated for ResultSet
C
It automatically closes the ResultSet
D
It enables caching of all rows in memory
Correct Answer:
A. It determines the maximum number of rows to fetch from database in one round-trip
EXPLANATION
FetchSize controls how many rows are fetched from the database at once, optimizing performance for large datasets.
Which JDBC feature should be used for executing multiple SQL statements in a batch for optimal performance?
A
executeUpdate() in a loop
B
executeBatch() with addBatch()
C
execute() method
D
Multiple Statement objects
Correct Answer:
B. executeBatch() with addBatch()
EXPLANATION
executeBatch() with addBatch() reduces network round-trips and improves performance for bulk operations.
In a JDBC application, when using transactions with setAutoCommit(false), what happens to intermediate savepoints if rollback() is called?
A
All changes are saved
B
Only savepoint changes are rolled back
C
All changes since last commit are rolled back
D
The entire connection is closed
Correct Answer:
C. All changes since last commit are rolled back
EXPLANATION
When rollback() is called without savepoints, all changes made since the last commit are undone and rolled back.
Consider a scenario: A developer uses Statement with user input directly in SQL queries. What is the primary risk?
A
Slower query execution
B
SQL Injection attacks
C
Memory leaks
D
Connection timeout
Correct Answer:
B. SQL Injection attacks
EXPLANATION
Direct concatenation of user input in SQL queries exposes the application to SQL injection attacks. PreparedStatement should be used instead.
In a multi-threaded JDBC application, why should each thread have its own Connection object?
A
To improve database performance
B
Connections are not thread-safe and sharing causes race conditions
C
To reduce memory usage
D
It is just a best practice with no technical requirement
Correct Answer:
B. Connections are not thread-safe and sharing causes race conditions
EXPLANATION
JDBC Connections are not thread-safe. Each thread must have its own Connection to prevent concurrent access issues and data corruption.
What is the optimal approach to handle JDBC resources to prevent memory leaks in a production application?
A
Use try-catch blocks and manually close resources
B
Rely on garbage collection to close resources
C
Use try-with-resources statement to auto-close resources
D
Create resources globally and close them at application shutdown
Correct Answer:
C. Use try-with-resources statement to auto-close resources
EXPLANATION
Try-with-resources (try-with-resources statement) automatically closes AutoCloseable resources, preventing memory leaks.
Consider a scenario where a developer retrieves a ResultSet and then closes the Statement object. What happens to the ResultSet?
A
The ResultSet remains valid and can still be accessed
B
The ResultSet becomes invalid in most drivers (implementation-dependent)
C
An immediate SQLException is thrown
D
The database automatically commits pending changes
Correct Answer:
B. The ResultSet becomes invalid in most drivers (implementation-dependent)
EXPLANATION
Closing a Statement typically invalidates its associated ResultSet in most JDBC drivers, though behavior may vary.
A developer creates a JDBC connection but forgets to close it. What might be the consequence?
A
Automatic garbage collection will close it immediately
B
Resource leak leading to exhaustion of database connections
C
The program will automatically rollback all transactions
D
No impact as JDBC handles it automatically
Correct Answer:
B. Resource leak leading to exhaustion of database connections
EXPLANATION
Unclosed connections remain allocated, eventually exhausting the connection pool and causing application failures.
In JDBC, what is the purpose of using a SavePoint in a transaction?
A
To save the current connection state
B
To create a checkpoint within a transaction that can be rolled back to independently
C
To automatically commit pending changes
D
To lock the database for exclusive access
Correct Answer:
B. To create a checkpoint within a transaction that can be rolled back to independently
EXPLANATION
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.