JDBC Quiz - Multiple Choice Questions

Take this JDBC Quiz to test your self JDBC knowledge.

JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.
Learn complete JDBC at https://www.javaguides.net/p/jdbc-tutorial.html.

1. 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.

2. 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: Connection is an interface. Since interfaces do not have constructors, Option D is incorrect. The Connection class doesn’t have a static method to get a Connection either, making Option A incorrect. The Driver class is also an interface without static methods, making Option B incorrect. Option C is the answer because DriverManager is the class used in JDBC to get a Connection.

3. Which is responsible for getting a connection to the database?

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

Answer

A. Driver
Explanation: The Driver interface is responsible for getting a connection to the database, making Option A the answer. The Connection interface is responsible for communication with the database but not making the initial connection. The Statement interface knows how to run the SQL query, and the ResultSet interface knows what was returned by a SELECT query

4. What is the output when run with a JDBC 4.0 driver if the "demo" database exists and contains an empty "users" table?

String url = "jdbc:derby:demo";
try (Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select count(*) from users")) {
    System.out.println(rs.getInt(1));
}
A. 0
B. 1
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

Answer

D. The code compiles but throws an exception at runtime.
Explanation: This code is missing a call to rs.next(). As a result, rs.getInt(1) throws an SQLException with the message Invalid cursor state – no current row. Therefore, Option D is the answer.

5. 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 code you write

6. How many rows are added to the colors table from running the following?

try (Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement()) {
    conn.setAutoCommit(false);
    stmt.executeUpdate("insert into colors values ('red')");
    stmt.executeUpdate("insert into colors values ('blue')");
    conn.commit();
    conn.setAutoCommit(true);
    stmt.executeUpdate("insert into colors values ('green')");
}
A. None
B. One
C. Two
D. Three

Answer

B. One
Explanation: The code turns off automatic committing, so the inserts for red and blue are not immediately made. The rollback(a) statement actually prevents them from being committed. Then automatic commit is turned back on and one insert is made, making Option B the answer.

7. 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.

8. Which of the following is not a valid type of ResultSet?

A - ResultSet.TYPE_FORWARD_ONLY
B - ResultSet.TYPE_SCROLL_INSENSITIVE
C - ResultSet.TYPE_SCROLL_SENSITIVE
D - ResultSet.TYPE_BACKWARD_ONLY

Answer

 D
Explanation: ResultSet.TYPE_BACKWARD_ONLY is not a valid type of ResultSet.

9. Which JDBC driver type is the JDBC-ODBC type?

  1. Type 1
  2. Type 2
  3. Type 3
  4. Type 4

10. Which of the following is efficient than a statement due to the pre-compilation of SQL?

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

Answer

 B
Explanation: PreparedStatement is efficient than the statement due to the pre-compilation of SQL.
Learn complete JDBC at https://www.javaguides.net/p/jdbc-tutorial.html.

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

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments