Chapter 21 JDBC Review Questions Flashcards
1. Which interfaces or classes are in a database‐specific JAR file?
(Choose all that apply.)
A. Driver
B. Driver’s implementation
C. DriverManager
D. DriverManager’s implementation
E. PreparedStatement
F. PreparedStatement’s implementation
Wrong: B, D, F
B, F.
* The Driver and PreparedStatement interfaces are part of the JDK, making options A and E incorrect.
* The concrete DriverManager class is also part of the JDK, making options C and D incorrect.
* Options B and F are correct since the implementation of these interfaces is part of the database-specific driver JAR file.
2. Which are required parts of a JDBC URL? (Choose all that apply.)
A. Connection parameters
B. IP address of database
C. jdbc
D. Password
E. Port
F. Vendor‐specific string
wrong: B, C, D, E, F
C, F.
* A JDBC URL has three parts.
* The first part is the string jdbc, making option C correct.
* The second part is the subprotocol. This is the vendor/product name, which isn’t an answer choice.
* The subname is vendor-specific, making option F correct as well.
3. Which of the following is a valid JDBC URL?
A. jdbc:sybase:localhost:1234/db
B. jdbc::sybase::localhost::/db
C. jdbc::sybase:localhost::1234/db
D. sybase:localhost:1234/db
E. sybase::localhost::/db
F. sybase::localhost::1234/db
A.
* A JDBC URL has three main parts separated by single colons, making options B, C, E, and F incorrect.
* The first part is always jdbc, making option D incorrect.
* Therefore, the correct answer is option A. Notice that you can get this right even if you’ve never heard of the Sybase database before.
4. Which of the options can fill in the blank to make the code compile and run without error? (Choose all that apply.)
var sql = "UPDATE habitat WHERE environment = ?"; try (var ps = conn.prepareStatement(sql)) { \_\_\_\_\_\_\_\_\_ ps.executeUpdate(); }
A. ps.setString(0, “snow”);
B. ps.setString(1, “snow”);
C. ps.setString(“environment”, “snow”);
D. ps.setString(1, “snow”); ps.setString(1, “snow”);
E. ps.setString(1, “snow”); ps.setString(2, “snow”);
F. ps.setString(“environment”, “snow”);ps.setString(“environment”, “snow”);
B, D.
* When setting parameters on a PreparedStatement, there are only options that take an index, making options C and F incorrect.
* The indexing starts with 1, making option A incorrect.
* This query has only one parameter, so option E is also incorrect.
* Option B is correct because it simply sets the parameter.
* Option D is also correct because it sets the parameter and then immediately overwrites it with the same value.
5. Suppose that you have a table named animal with two rows. What is the result of the following code?
6: var conn = new Connection(url, userName, password); 7: var ps = conn.prepareStatement( 8: "SELECT count(*) FROM animal"); 9: var rs = ps.executeQuery(); 10: if (rs.next()) System.out.println(rs.getInt(1));
A. 0
B. 2
C. There is a compiler error on line 6.
D. There is a compiler error on line 10.
E. There is a compiler error on another line.
F. A runtime exception is thrown.
C.
- A Connection is created using a static method on DriverManager. It does not use a constructor. Therefore, option C is correct. If the Connection was created properly, the answer would be option B.
6. Which of the options can fill in the blanks in order to make the code compile?
boolean bool = ps.(); int num = ps.(); ResultSet rs = ps.();
A. execute, executeQuery, executeUpdate
B. execute, executeUpdate, executeQuery
C. executeQuery, execute, executeUpdate
D. executeQuery, executeUpdate, execute
E. executeUpdate, execute, executeQuery
F. executeUpdate, executeQuery, execute
B.
* The first line has a return type of boolean, making it an execute() call.
* The second line returns the number of modified rows, making it an executeUpdate() call.
* The third line returns the results of a query, making it an executeQuery() call.
7. Which of the following are words in the CRUD acronym? (Choose all that apply.)
A. Create
B. Delete
C. Disable
D. Relate
E. Read
F. Upgrade
A, B, E.
CRUD stands for Create, Read, Update, Delete, making options A, B, and E correct.
8. Suppose that the table animal has five rows and the following SQL statement updates all of them. What is the result of this code?
public static void main(String[] args) throws SQLException { var sql = "UPDATE names SET name = 'Animal'"; try (var conn = DriverManager.getConnection("jdbc:derby:zoo"); var ps = conn.prepareStatement(sql)) { var result = ps.executeUpdate(); System.out.println(result); } }
A. 0
B. 1
C. 5
D. The code does not compile.
E. A SQLException is thrown.
F. A different exception is thrown.
C.
This code works as expected. It updates each of the five rows in the table and returns the number of rows updated. Therefore, option C is correct.
9. Suppose learn() is a stored procedure that takes one IN parameter. What is wrong with the following code? (Choose all that apply.)
18: var sql = "call learn()"; 19: try (var cs = conn.prepareCall(sql)) { 20: cs.setString(1, "java"); 21: try (var rs = cs.executeQuery()) { 22: while (rs.next()) { 23: System.out.println(rs.getString(3)); 24: } 25: } 26: }
A. Line 18 is missing braces.
B. Line 18 is missing a ?.
C. Line 19 is not allowed to use var.
D. Line 20 does not compile.
E. Line 22 does not compile.
F. Something else is wrong with the code.
G. None of the above. This code is correct.
A, B.
* Option A is one of the answers because you are supposed to use braces ({}) for all SQL in a CallableStatement.
* Option B is the other answer because each parameter should be passed with a question mark (?).
* The rest of the code is correct. Note that your database might not behave the way that’s described here, but you still need to know this syntax for the exam.
10. Suppose that the table enrichment has three rows with the animals bat, rat, and snake. How many lines does this code print?
var sql = "SELECT toy FROM enrichment WHERE animal = ?"; try (var ps = conn.prepareStatement(sql)) { ps.setString(1, "bat"); try (var rs = ps.executeQuery(sql)) { while (rs.next()) System.out.println(rs.getString(1)); } }
A. 0
B. 1
C. 3
D. The code does not compile.
E. A SQLException is thrown.
F. A different exception is thrown.
Wrong: B
E.
The code compiles because PreparedStatement extends Statement and Statement allows passing a String in the executeQuery() call. While PreparedStatement can have bind variables, Statement cannot. Since this code uses executeQuery(sql)
in Statement, it fails at runtime. A SQLException
is thrown, making option E correct.
11. Suppose that the table food has five rows and this SQL statement updates all of them. What is the result of this code?
public static void main(String[] args) { var sql = "UPDATE food SET amount = amount + 1"; try (var conn = DriverManager.getConnection("jdbc:derby:zoo"); var ps = conn.prepareStatement(sql)) { var result = ps.executeUpdate(); System.out.println(result); } }
A. 0
B. 1
C. 5
D. The code does not compile.
E. A SQLException is thrown.
F. A different exception is thrown.
Wrong : C
D.
* JDBC code throws a SQLException, which is a checked exception. The code does not handle or declare this exception, and therefore it doesn’t compile. Since the code doesn’t compile, option D is correct.
* If the exception were handled or declared, the answer would be option C.
12.Suppose we have a JDBC program that calls a stored procedure, which returns a set of results. Which is the correct order in which to close database resources for this call?
A. Connection, ResultSet, CallableStatement
B. Connection, CallableStatement, ResultSet
C. ResultSet, Connection, CallableStatement
D. ResultSet, CallableStatement, Connection
E. CallableStatement, Connection, ResultSet
F. CallableStatement, ResultSet, Connection
D.
JDBC resources should be closed in the reverse order from that in which they were opened. The order for opening is Connection, CallableStatement, and ResultSet. The order for closing is ResultSet, CallableStatement, and Connection.
13. Suppose that the table counts has five rows with the numbers 1 to 5. How many lines does this code print?
var sql = "SELECT num FROM counts WHERE num> ?"; try (var ps = conn.prepareStatement(sql)) { ps.setInt(1, 3); try (var rs = ps.executeQuery()) { while (rs.next()) System.out.println(rs.getObject(1)); } ps.setInt(1, 100); try (var rs = ps.executeQuery()) { while (rs.next()) System.out.println(rs.getObject(1)); } }
A. 0
B. 1
C. 2
D. 4
E. The code does not compile.
F. The code throws an exception.
C.
- This code calls the PreparedStatement twice.
- The first time, it gets the numbers greater than 3. Since there are two such numbers, it prints two lines.
- The second time, it gets the numbers greater than 100. There are no such numbers, so the ResultSet is empty.
- A total of two lines is printed, making option C correct.
14. Which of the following can fill in the blank correctly? (Choose all that apply.)
var rs = ps.executeQuery(); if (rs.next()) { \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_; }
A. String s = rs.getString(0)
B. String s = rs.getString(1)
C. String s = rs.getObject(0)
D. String s = rs.getObject(1)
E. Object s = rs.getObject(0)
F. Object s = rs.getObject(1)
Wrong : B, D, F
B, F.
- In a ResultSet, columns are indexed starting with 1, not 0. Therefore, options A, C, and E are incorrect.
- There are methods to get the column as a String or Object. However, option D is incorrect because an Object cannot be assigned to a String without a cast.
15. Suppose learn() is a stored procedure that takes one IN parameter and one OUT parameter. What is wrong with the following code? (Choose all that apply.)
18: var sql = "{?= call learn(?)}"; 19: try (var cs = conn.prepareCall(sql)) { 20: cs.setInt(1, 8); 21: cs.execute(); 22: System.out.println(cs.getInt(1)); 23: }
A. Line 18 does not call the stored procedure properly.
B. The parameter value is not set for input.
C. The parameter is not registered for output.
D. The code does not compile.
E. Something else is wrong with the code.
F. None of the above. This code is correct.
C.
Since an OUT parameter is used, the code should call registerOutParameter(). Since this is missing, option C is correct.C