Java JDBC MCQ

Java Database Connectivity (JDBC) is a Java API that allows Java applications to interact with databases. It’s a key technology for any Java developer working with relational databases. Whether you are just starting out or looking to refresh your JDBC knowledge, this quiz will test your understanding of the basics.

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. Which package is primarily used for JDBC classes and interfaces?

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

Answer:

a) java.sql

Explanation:

JDBC classes and interfaces are primarily located in the java.sql package.

2. Which interface is used to execute SQL queries?

a) DatabaseExecutor
b) Statement
c) QueryExecutor
d) SqlConnection

Answer:

b) Statement

Explanation:

The Statement interface is used to execute SQL queries in JDBC.

3. What does JDBC DriverManager class do?

a) Manages Java threads
b) Manages database drivers
c) Manages database cache
d) Manages SQL transactions

Answer:

b) Manages database drivers

Explanation:

The DriverManager class manages a list of database drivers.

4. Which method is used to establish a connection to the database?

a) connect()
b) getConnection()
c) establishConnection()
d) openDbConnection()

Answer:

b) getConnection()

Explanation:

The getConnection() method of the DriverManager class is used to establish a connection to the database.

5. How can you execute a stored procedure using JDBC?

a) Using the Statement class
b) Using the PreparedStatement class
c) Using the CallableStatement class
d) Using the ProcedureStatement class

Answer:

c) Using the CallableStatement class

Explanation:

The CallableStatement interface is used to execute stored procedures in JDBC.

6. Which of the following is not a JDBC type?

a) TYPE_SCROLL_SENSITIVE
b) TYPE_SCROLL_INSENSITIVE
c) TYPE_SCROLL_AWARE
d) TYPE_FORWARD_ONLY

Answer:

c) TYPE_SCROLL_AWARE

Explanation:

JDBC does not define a TYPE_SCROLL_AWARE type. The other options are valid JDBC ResultSet types.

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

a) commit()
b) execute()
c) save()
d) finalize()

Answer:

a) commit()

Explanation:

The commit() method of the Connection interface is used to commit a transaction.

8. How do you roll back a transaction in JDBC?

a) Using the rollback() method
b) Using the undo() method
c) Setting auto-commit to true
d) Using the back() method

Answer:

a) Using the rollback() method

Explanation:

The rollback() method of the Connection interface is used to roll back a transaction.

9. Which interface handles the result of a query?

a) QuerySet
b) DbResult
c) ResultSet
d) QueryResult

Answer:

c) ResultSet

Explanation:

The ResultSet interface is used to handle the result returned by a query.

10. Which method is used to update data in a ResultSet?

a) updateRow()
b) modifyRow()
c) saveRow()
d) setRow()

Answer:

a) updateRow()

Explanation:

The updateRow() method of the ResultSet interface is used to reflect changes made to the current row in the database.

11. Which of these is not a JDBC driver type?

a) Thin driver
b) ODBC driver
c) Thick driver
d) Native-API driver

Answer:

c) Thick driver

Explanation:

There is no JDBC driver known as “Thick driver.” The others are types or categories of JDBC drivers.

12. Which class can be used to retrieve metadata of a database?

a) DatabaseInfo
b) DbMetaData
c) DatabaseMetaData
d) MetaData

Answer:

c) DatabaseMetaData

Explanation:

The DatabaseMetaData interface provides methods to get information about the database.

13. What is the default transaction isolation level in JDBC?

a) READ_COMMITTED
b) READ_UNCOMMITTED
c) SERIALIZABLE
d) REPEATABLE_READ

Answer:

a) READ_COMMITTED

Explanation:

The default transaction isolation level in JDBC is READ_COMMITTED.

14. Which method can be used to set auto-commit mode in JDBC?

a) setAutoCommit(boolean)
b) autoCommit(boolean)
c) enableAutoCommit(boolean)
d) commitAutomatically(boolean)

Answer:

a) setAutoCommit(boolean)

Explanation:

The setAutoCommit(boolean) method of the Connection interface is used to set the auto-commit mode in JDBC.

15. What does the executeQuery() method of the Statement interface return?

a) An integer
b) A boolean
c) ResultSet
d) PreparedStatement

Answer:

c) ResultSet

Explanation:

The executeQuery() method of the Statement interface returns a ResultSet object which contains the result of the query.

Understanding JDBC is crucial for Java developers working with relational databases. This quiz provides a glimpse into some basic JDBC concepts. As you advance in your Java journey, delve deeper into this topic, explore advanced features, and practice to gain hands-on experience. Happy coding!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top