Java JDBC MCQ

Check out our latest blog post for a fantastic quiz on Java JDBC. It’s the perfect way to test your database skills and learn more about Java’s way of handling databases!

Java JDBC (Java Database Connectivity) is a Java API that manages connecting to a database, executing queries and updates, and handling result sets obtained from the database. It provides a universal bridge between Java applications and data sources, allowing developers to interact with a wide range of databases, including Oracle, SQL Server, MySQL, and many others, using standard SQL queries.

Dive into our MCQs to sharpen your Java JDBC skills. Whether you’re prepping for a job interview, studying for exams, or just curious about database connectivity in Java, this quiz is an excellent tool for boosting your understanding and expertise. Ready to connect with some knowledge? Let’s get started!

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