JDBC Quiz - Multiple Choice Questions (MCQ)

JDBC (Java Database Connectivity) is a standard API for connecting Java applications to databases and executing SQL queries. In this blog post, we present a JDBC Quiz consisting of 15+ 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 Binding Connection 
c) Java Database Control 
d) Java Data Business Connector 

Answer:

a) Java Database Connectivity 

Explanation:

JDBC stands for Java Database Connectivity, which provides a standard API for connecting Java applications to databases. 

2. Which class is used to establish a database connection in JDBC? 

a) java.sql.Connection 
b) java.sql.DriverManager 
c) java.sql.Statement 
d) java.sql.ResultSet 

Answer:

b) java.sql.DriverManager 

Explanation:

The java.sql.DriverManager class is used to establish a database connection in JDBC. It provides methods for registering drivers and creating connections to a database. 

3. What is the purpose of a PreparedStatement in JDBC? 

a) To execute SQL queries and retrieve results 
b) To insert, update, or delete data in a database 
c) To retrieve metadata about a database 
d) To handle exceptions in JDBC operations 

Answer:

b) To insert, update, or delete data in a database 

Explanation:

A PreparedStatement in JDBC is used to execute parameterized SQL statements for inserting, updating, or deleting data in a database. 

4. What is the role of java.sql.ResultSet interface in JDBC? 

a) To establish a connection to the database 
b) To execute SQL statements 
c) To represent the result of a database query 
d) To handle exceptions in JDBC operations 

Answer:

c) To represent the result of a database query 

Explanation:

The java.sql.ResultSet interface in JDBC is used to represent the result of a database query. It provides methods for navigating and accessing the retrieved data. 

5. Which method is used to execute an SQL query in JDBC? 

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

Answer:

a) executeQuery() 

Explanation:

The executeQuery() method in JDBC is used to execute an SQL query and retrieve the result as a ResultSet object. 

6. What is the purpose of JDBC batch processing? 

a) To execute multiple SQL statements in a single transaction 
b) To improve performance by reducing network round trips 
c) To handle database connection pooling 
d) To handle concurrency in multi-threaded applications 

Answer:

b) To improve performance by reducing network round trips 

Explanation:

JDBC batch processing is used to improve performance by reducing network round trips. It allows multiple SQL statements to be grouped and sent to the database for execution as a batch. 

7. How do you handle exceptions in JDBC? 

a) By using try-catch blocks 
b) By using the throws clause in method signatures 
c) By using the catch clause in SQL statements 
d) By using the throw statement 

Answer:

a) By using try-catch blocks 

Explanation:

Exceptions in JDBC can be handled by using try-catch blocks to catch and handle specific exceptions that may occur during database operations. 

8. Which JDBC interface is responsible for managing transactions in JDBC? 

a) java.sql.Connection 
b) java.sql.Statement 
c) java.sql.ResultSet 
d) java.sql.Transaction 

Answer:

a) java.sql.Connection 

Explanation:

The java.sql.Connection interface in JDBC is responsible for managing transactions. It provides methods for starting, committing, and rolling back transactions. 

9. What must be the first characters of a database URL?

A. db,
B. db:
C. jdbc,
D. jdbc: 

Answer:

D. jdbc: 

Explanation:

All JDBC URLs begin with the protocol jdbc followed by a colon as a delimiter. Option D is the only one that does both of these, making it the answer.

10. Which of these obtains a Connection?

A. Connection.getConnection(url)
B. Driver.getConnection(url)
C. DriverManager.getConnection(url)
D. new Connection(url) 

Answer:

C. DriverManager.getConnection(url)

Explanation:

Option C is the correct answer because DriverManager is the class used in JDBC to get a Connection.

11. Which resources have their close() method called when this code runs?

public static void runQuery(Connection conn) throws SQLException {
    try (Statement stmt = conn.createStatement()) {
        ResultSet rs = stmt.executeQuery("select * from clowns");
        rs.next();
    }
}
A. No close() methods are called.
B. Only Statement
C. Only Statement and Connection
D. Only Statement and ResultSet 

Answer:

D. Only Statement and ResultSet 

Explanation:

Since this code opens Statement using a try-with-resources, Statement gets closed automatically at the end of the block. Further, closing a Statement automatically closes a ResultSet created by it, making Option D the answer. Remember that you should close any resources you open in the code you write.

12. What is the correct order to close database resources?

A. Connection then Statement then ResultSet
B. ResultSet then Statement then Connection
C. Statement then Connection then ResultSet
D. Statement then ResultSet then Connection

Answer:

B. ResultSet then Statement then Connection

Explanation:

When manually closing database resources, they should be closed in the reverse order from which they were opened. This means that the ResultSet object is closed before the Statement object and the Statement object is closed before the Connection object. This makes Option B the answer.

13. Which of the following is more efficient than a Statement due to the pre-compilation of SQL? 

A. Statement
B. PreparedStatement
C. CallableStatement
D. None of the above.

Answer:

B. PreparedStatement

Explanation:

The PreparedStatement is more efficient than the Statement due to the pre-compilation of SQL.

14. Which of the following is a new feature introduced in JDBC Version 4? 

a) Connection pooling 
b) Distributed transactions 
c) Row-level locking 
d) Automatic driver loading and registration

Answer:

d) Automatic driver loading and registration

Explanation:

JDBC Version 4 introduced automatic driver loading and registration, eliminating the need for explicit driver class loading and registration using Class.forName().

15. JDBC Version 4 introduced a new feature called "Scrollable Result Sets." What is the purpose of scrollable result sets? 

a) To allow bidirectional traversal of the result set 
b) To fetch only a subset of rows from the result set 
c) To provide metadata information about the result set 
d) To handle batch processing of SQL statements 

Answer:

a) To allow bidirectional traversal of the result set 

Explanation:

Scrollable Result Sets in JDBC Version 4 allow bidirectional traversal of the result set, enabling you to navigate forward and backward through the rows. 

16. JDBC Version 4 introduced support for retrieving automatically generated keys after executing an INSERT statement. Which method is used to retrieve the generated keys? 

a) executeQuery() 
b) getGeneratedKeys() 
c) executeUpdate() 
d) getResultSet() 

Answer:

b) getGeneratedKeys() 

Explanation:

The getGeneratedKeys() method in JDBC Version 4 is used to retrieve the automatically generated keys after executing an INSERT statement.

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!

Related Posts

  1. Java String Quiz
  2. Java Arrays Quiz
  3. Java Loops Quiz
  4. Java OOPS Quiz
  5. Java OOPS Quiz - Part 1
  6. Java OOPS Quiz - Part 2
  7. Java Exception Handling Quiz
  8. Java Collections Quiz
  9. Java Generics Quiz
  10. JDBC Quiz
  11. Java Lambda Expressions Quiz
  12. Java Functional Interfaces Quiz
  13. Java Streams API Quiz
  14. Java Date Time Quiz
  15. Java 8 Quiz

Comments