JDBC Quiz - Multiple Choice Questions (MCQ)


JDBC (Java Database Connectivity) is a standard API for connecting Java applications to databases and executing SQL queries. This blog post presents a JDBC Quiz consisting of 25+ multiple-choice questions (MCQ). 

This quiz aims to assess your understanding of JDBC concepts, including database connections, SQL statements, result sets, new features, and exception handling. Let's put your knowledge of JDBC to the test!

Learn everything about JDBC: Java JDBC Tutorial.

1. What does JDBC stand for?

a) Java DataBase Connectivity
b) Java Data Binary Connector
c) Java DataBase Collection
d) Java Data Buffering Class

Answer:

a) Java DataBase Connectivity

Explanation:

JDBC stands for Java DataBase Connectivity. It is a Java API that allows Java applications to interact with databases.

2. Which package contains the JDBC classes and interfaces?

a) java.jdbc
b) java.db
c) java.sql
d) javax.sql

Answer:

c) java.sql

Explanation:

The java.sql package contains the JDBC classes and interfaces necessary for database connectivity in Java.

3. What is the purpose of the DriverManager class in JDBC?

a) To manage database drivers
b) To manage database connections
c) To manage database queries
d) To manage database transactions

Answer:

b) To manage database connections

Explanation:

The DriverManager class in JDBC is used to manage database connections by selecting the appropriate driver and establishing a connection with the database.

4. How do you load a JDBC driver in Java?

a) Class.forName("driverClassName")
b) Driver.load("driverClassName")
c) DriverManager.loadDriver("driverClassName")
d) Database.loadDriver("driverClassName")

Answer:

a) Class.forName("driverClassName")

Explanation:

The Class.forName("driverClassName") method is used to load the JDBC driver class into memory, making it available for use in the application.

5. Which method is used to establish a connection to a database in JDBC?

a) DriverManager.getConnection()
b) DriverManager.connect()
c) Driver.connect()
d) Connection.open()

Answer:

a) DriverManager.getConnection()

Explanation:

The DriverManager.getConnection() method is used to establish a connection to a database by providing the database URL, username, and password.

6. What is a PreparedStatement in JDBC?

a) A statement that is compiled and stored in the database
b) A statement that cannot be reused
c) A statement that can be reused with different input values
d) A statement that does not return any result

Answer:

c) A statement that can be reused with different input values

Explanation:

A PreparedStatement in JDBC is a precompiled SQL statement that can be reused multiple times with different input values, providing better performance and security.

7. Which interface is used to execute SQL queries in JDBC?

a) Statement
b) PreparedStatement
c) CallableStatement
d) All of the above

Answer:

d) All of the above

Explanation:

All of the mentioned interfaces (Statement, PreparedStatement, and CallableStatement) are used to execute SQL queries in JDBC.

8. Which method is used to execute a SQL SELECT query in JDBC?

a) executeQuery()
b) executeUpdate()
c) execute()
d) executeSelect()

Answer:

a) executeQuery()

Explanation:

The executeQuery() method is used to execute SQL SELECT queries, which return a ResultSet containing the result of the query.

9. What does the ResultSet interface represent in JDBC?

a) A database connection
b) A statement that returns a result
c) A table of data representing the result of a SQL query
d) A transaction manager

Answer:

c) A table of data representing the result of a SQL query

Explanation:

The ResultSet interface represents a table of data that is the result of executing a SQL query. It allows the program to iterate through the rows and retrieve data.

10. Which method is used to execute a SQL INSERT, UPDATE, or DELETE query in JDBC?

a) executeQuery()
b) executeUpdate()
c) execute()
d) executeModify()

Answer:

b) executeUpdate()

Explanation:

The executeUpdate() method is used to execute SQL INSERT, UPDATE, or DELETE queries, which modify the database but do not return a ResultSet.

11. Which method is used to retrieve data from a ResultSet in JDBC?

a) getString()
b) getInt()
c) getDate()
d) All of the above

Answer:

d) All of the above

Explanation:

All of the mentioned methods (getString(), getInt(), getDate()) are used to retrieve data from a ResultSet in JDBC. The method used depends on the type of data being retrieved.

12. What is the purpose of the setAutoCommit() method in JDBC?

a) To automatically commit each SQL statement
b) To automatically rollback each SQL statement
c) To manually commit SQL statements
d) To set the transaction isolation level

Answer:

a) To automatically commit each SQL statement

Explanation:

The setAutoCommit() method in JDBC is used to set the auto-commit mode. When auto-commit is enabled, each SQL statement is committed automatically after execution.

13. Which method is used to commit a transaction in JDBC?

a) commitTransaction()
b) commit()
c) executeCommit()
d) commitUpdate()

Answer:

b) commit()

Explanation:

The commit() method is used to commit a transaction in JDBC, making all changes made during the transaction permanent.

14. What does the rollback() method do in JDBC?

a) It saves the current transaction state
b) It commits the current transaction
c) It undoes all changes made in the current transaction
d) It ends the current transaction

Answer:

c) It undoes all changes made in the current transaction

Explanation:

The rollback() method in JDBC is used to undo all changes made during the current transaction, restoring the database to its previous state.

15. Which feature was introduced in JDBC 4.0?

a) Automatic driver loading
b) Improved BLOB and CLOB handling
c) Connection and Statement interface enhancements
d) All of the above

Answer:

d) All of the above

Explanation:

JDBC 4.0 introduced several features, including automatic driver loading, improved BLOB and CLOB handling, and enhancements to the Connection and Statement interfaces.

16. What is the purpose of the isValid() method in JDBC 4.0?

a) To check if a SQL query is valid
b) To check if a connection is still valid
c) To check if a ResultSet is valid
d) To check if a transaction is valid

Answer:

b) To check if a connection is still valid

Explanation:

The isValid() method in JDBC 4.0 is used to check if a database connection is still valid (i.e., not closed or invalidated).

17. Which JDBC feature allows you to execute a SQL query asynchronously?

a) AsyncStatement
b) AsyncQuery
c) CompletionStage
d) Statement.executeAsync()

Answer:

d) Statement.executeAsync()

Explanation:

The Statement.executeAsync() method allows executing a SQL query asynchronously, enabling non-blocking database operations.

18. What is the purpose of the getGeneratedKeys() method in JDBC?

a) To retrieve the primary keys of all tables
b) To retrieve the auto-generated keys after an INSERT operation
c) To retrieve foreign keys of a table
d) To retrieve all keys in the database

Answer:

b) To retrieve the auto-generated keys after an INSERT operation

Explanation:

The getGeneratedKeys() method is used to retrieve the auto-generated keys, such as primary keys, after executing an INSERT operation.

19. Which JDBC feature provides a standard way to batch multiple SQL statements?

a) SQLBatch
b) BatchProcessing
c) StatementBatch
d) BatchUpdate

Answer:

d) BatchUpdate

Explanation:

The BatchUpdate feature in JDBC provides a standard way to batch multiple SQL statements and execute them as a single batch for improved performance.

20. What does the JDBC 4.2 feature try-with-resources enable?

a) Automatic closing of JDBC resources
b) Simplified transaction management
c) Enhanced error handling
d) Improved connection pooling

Answer:

a) Automatic closing of JDBC resources

Explanation:

The try-with-resources feature introduced in JDBC 4.2 enables the automatic closing of JDBC resources like connections, statements, and ResultSets when they are no longer needed.

21. How does the Connection pool feature improve performance in JDBC?

a) By reusing database connections
b) By caching SQL queries
c) By parallelizing SQL queries
d) By optimizing SQL statements

Answer:

a) By reusing database connections

Explanation:

Connection pooling in JDBC improves performance by reusing existing database connections, reducing the overhead of establishing new connections.

22. Which JDBC feature allows accessing large binary and text files?

a) BLOB and CLOB support
b) LargeObject support
c) FileStream support
d) FileBlob support

Answer:

a) BLOB and CLOB support

Explanation:

The BLOB (Binary Large Object) and CLOB (Character Large Object) features in JDBC allow accessing and manipulating large binary and text files in a database.

23. Which JDBC feature allows handling large result sets efficiently?

a) Streaming ResultSets
b) Batch ResultSets
c) Cached ResultSets
d) Paged ResultSets

Answer:

a) Streaming ResultSets

Explanation:

Streaming ResultSets in JDBC allow handling large result sets efficiently by fetching rows one by one as needed, instead of loading the entire result set into memory at once.

24. What is the purpose of the setFetchSize() method in JDBC?

a) To set the maximum number of rows returned by a query
b) To set the number of rows fetched at a time from the database
c) To set the size of the database buffer
d) To set the number of connections in the connection pool

Answer:

b) To set the number of rows fetched at a time from the database

Explanation:

The setFetchSize() method in JDBC is used to set the number of rows fetched at a time from the database, improving performance for large result sets.

25. Which JDBC feature introduced in JDBC 4.x allows working with XML data directly?

a) SQLXML interface
b) XMLHandler
c) XMLProcessor
d) XMLSupport

Answer:

a) SQLXML interface

Explanation:

The SQLXML interface introduced in JDBC 4.x allows working with XML data directly in a database, enabling the storage and retrieval of XML documents as part of database operations.

Conclusion

Congratulations on completing the JDBC Quiz! We hope it challenged your knowledge and provided valuable insights into JDBC concepts. JDBC is a crucial technology for interacting with databases in Java applications. Learn everything about JDBC: Java JDBC Tutorial.
Keep practicing, keep learning, and become proficient in leveraging JDBC for database connectivity!



Comments