What does the DriverManager.getConnection() method return?
A A Statement object B A Connection object C A ResultSet object D A DatabaseMetaData object
DriverManager.getConnection() establishes a connection to the database and returns a Connection object.
In JDBC, which exception is thrown when the database driver class is not found?
A SQLException B ClassNotFoundException C DriverNotFoundException D DatabaseException
ClassNotFoundException is thrown when Class.forName() cannot locate the JDBC driver class.
What is the purpose of the Connection.commit() method in JDBC?
A To establish a new database connection B To permanently save all changes made in the current transaction C To execute the next SQL statement D To close the database connection
commit() finalizes the transaction and persists all changes made since the last commit or rollback.
Which JDBC method is used to retrieve metadata information about the database?
A getMetaData() B getDatabaseInfo() C getConnection().getInfo() D getProperties()
Connection.getMetaData() returns a DatabaseMetaData object containing information about the database and driver.
In a JDBC batch operation, what does executeBatch() return?
A A ResultSet object B An array of integers representing update counts C A single integer value D A boolean value
executeBatch() returns an int array where each element represents the number of rows affected by each statement in the batch.
What is the difference between ResultSet.TYPE_SCROLL_INSENSITIVE and ResultSet.TYPE_SCROLL_SENSITIVE?
A Insensitive is faster; Sensitive allows concurrent updates visibility B Insensitive allows backward navigation; Sensitive does not C Sensitive is read-only; Insensitive allows modifications D They are identical and can be used interchangeably
TYPE_SCROLL_INSENSITIVE doesn't reflect concurrent database changes, while TYPE_SCROLL_SENSITIVE may reflect such changes.
Which JDBC method is used to set an input parameter in a PreparedStatement?
A setParameter() B setInt(), setString(), etc. C bindParameter() D addParameter()
PreparedStatement uses type-specific setter methods like setInt(), setString(), setDate() to bind parameters.
What happens when Connection.setAutoCommit(false) is called in JDBC?
A Auto-commit is enabled immediately B Manual transaction control is enabled; changes require explicit commit C All pending transactions are rolled back D The connection is closed automatically
Setting autoCommit to false enables manual transaction management where commit() must be explicitly called.
In JDBC, what is the purpose of the ResultSetMetaData interface?
A To execute SQL queries B To retrieve information about the structure of a ResultSet C To manage database connections D To handle transaction management
ResultSetMetaData provides information about columns, data types, and column properties of a ResultSet.
Which JDBC constant specifies read-only access for a ResultSet?
A ResultSet.CONCUR_READ_ONLY B ResultSet.TYPE_READ_ONLY C ResultSet.MODE_READ D ResultSet.ACCESS_READ_ONLY
CONCUR_READ_ONLY is the concurrency constant that specifies a read-only ResultSet.
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
Connection pooling maintains a pool of reusable connections, reducing the cost of creating new connections repeatedly.
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
CallableStatement is used to call stored procedures and stored functions in the database.
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
Unclosed connections remain allocated, eventually exhausting the connection pool and causing application failures.
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
Closing a Statement typically invalidates its associated ResultSet in most JDBC drivers, though behavior may vary.
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
Try-with-resources (try-with-resources statement) automatically closes AutoCloseable resources, preventing memory leaks.
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
JDBC Connections are not thread-safe. Each thread must have its own Connection to prevent concurrent access issues and data corruption.
Which interface in JDBC is used to execute SQL queries and retrieve results?
A Statement B Connection C ResultSet D Driver
Statement interface is used to execute SQL queries. Connection creates statements, ResultSet holds results, and Driver manages connections.
Which JDBC API is used to execute parameterized queries safely?
A Statement B PreparedStatement C ResultSet D MetaData
PreparedStatement allows parameterized queries using placeholders (?) and prevents SQL injection attacks.
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');
Class.forName() is the standard way to load JDBC drivers. It dynamically loads the driver class.
Which method is used to retrieve the next row from a ResultSet?
A getNext() B next() C moveToNext() D fetchNext()
The next() method moves the cursor to the next row in the ResultSet and returns true if a row exists.