Showing 41–50 of 100 questions
in JDBC
Which method is used to retrieve the next row from a ResultSet?
A
getNext()
B
next()
C
moveToNext()
D
fetchNext()
Correct Answer:
B. next()
EXPLANATION
The next() method moves the cursor to the next row in the ResultSet and returns true if a row exists.
What is the correct syntax to load a JDBC driver in Java?
A
DriverManager.loadDriver('com.mysql.jdbc.Driver');
B
Class.forName('com.mysql.jdbc.Driver');
C
Driver.load('com.mysql.jdbc.Driver');
D
Connection.loadDriver('com.mysql.jdbc.Driver');
Correct Answer:
B. Class.forName('com.mysql.jdbc.Driver');
EXPLANATION
Class.forName() is the standard way to load JDBC drivers. It dynamically loads the driver class.
Which JDBC API is used to execute parameterized queries safely?
A
Statement
B
PreparedStatement
C
ResultSet
D
MetaData
Correct Answer:
B. PreparedStatement
EXPLANATION
PreparedStatement allows parameterized queries using placeholders (?) and prevents SQL injection attacks.
Which interface in JDBC is used to execute SQL queries and retrieve results?
A
Statement
B
Connection
C
ResultSet
D
Driver
Correct Answer:
A. Statement
EXPLANATION
Statement interface is used to execute SQL queries. Connection creates statements, ResultSet holds results, and Driver manages connections.
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 CallableStatement?
A
To execute simple SELECT queries
B
To execute stored procedures and functions
C
To batch multiple SQL statements
D
To manage transaction isolation levels
Correct Answer:
B. To execute stored procedures and functions
EXPLANATION
CallableStatement is used to call stored procedures and stored functions in the database.
What is Connection pooling in JDBC and why is it used?
A
A method to execute multiple SQL statements in parallel
B
Reusing database connections to improve performance and reduce overhead
C
A feature to automatically backup database connections
D
A mechanism to encrypt database connections
Correct Answer:
B. Reusing database connections to improve performance and reduce overhead
EXPLANATION
Connection pooling maintains a pool of reusable connections, reducing the cost of creating new connections repeatedly.