Home Subjects Java Programming JDBC

Java Programming
JDBC

Java OOP, collections, multithreading

25 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 25
Topics in Java Programming
Q.11 Hard JDBC
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.

Test
Q.12 Hard JDBC
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.

Test
Q.13 Hard JDBC
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.

Test
Q.14 Hard JDBC
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.

Test
Q.15 Hard JDBC
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.

Test
Q.16 Hard JDBC
A batch update operation fails partially. How can you identify which statements failed in the batch?
A Check the exception message
B Use the int array returned by executeBatch() to check individual update counts
C Re-execute the batch statement by statement
D Use savepoints for each batch statement
Correct Answer:  B. Use the int array returned by executeBatch() to check individual update counts
EXPLANATION

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.

Test
Q.17 Hard JDBC
Which method is used to check if a CallableStatement has a return value from a stored procedure?
A hasResult()
B wasNull()
C getReturnValue()
D getObject(int parameterIndex)
Correct Answer:  B. wasNull()
EXPLANATION

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().

Test
Q.18 Hard JDBC
What is the default transaction isolation level in JDBC when autocommit is disabled?
A TRANSACTION_READ_UNCOMMITTED
B TRANSACTION_READ_COMMITTED
C TRANSACTION_REPEATABLE_READ
D Depends on the database driver
Correct Answer:  D. Depends on the database driver
EXPLANATION

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).

Test
Q.19 Hard JDBC
A developer wants to prevent SQL injection attacks while executing dynamic queries. Which approach is most secure?
A Use String concatenation with input validation
B Use PreparedStatement with setString() methods
C Use regular expressions to filter input
D Use encrypted password fields in queries
Correct Answer:  B. Use PreparedStatement with setString() methods
EXPLANATION

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.

Test
Q.20 Hard JDBC
What is the difference between update() and updateRow() methods in ResultSet?
A update() modifies a single column, updateRow() commits all column changes
B update() is for SELECT queries, updateRow() is for UPDATE queries
C update() is for integers, updateRow() is for all data types
D There is no difference; they are synonymous
Correct Answer:  A. update() modifies a single column, updateRow() commits all column changes
EXPLANATION

update*() methods (like updateInt(), updateString()) modify individual column values in the current row, while updateRow() commits all these changes to the database.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips