IS5103 Lesson-15

1.Which is the first line containing a compiler error?
String url = “jdbc:hsqldb:file:zoo”;
try (var conn = DriverManager.getConnection(url);
var ps = conn.prepareStatement();
var rs = ps.executeQuery(“SELECT * FROM swings”)) {
while (rs.next()) {
System.out.println(rs.getInteger(1));
}
}
A) Line 3

2.Which interfaces or classes are in a database-specific JAR file?
Each correct answer represents a complete solution. Choose all that apply.
A) Driver’s implementation
PreparedStatement implementation

3.Which of the following is a valid JDBC URL?
A) jdbc:sybase:localhost:1234/db

4.Suppose that you have a table named animal with two rows. What is
the result of the following code?
var conn = new Connection(url, userName, password);
var ps = conn.prepareStatement(
“SELECT count(*) FROM animal”);
var rs = ps.executeQuery();
if (rs.next()) System.out.println(rs.getInt(1));
A) There is a compiler error on line 6.

5.Which of the following can fill in the blank?
var sql = “__________“;
try (var ps = conn.prepareStatement(sql)) {
ps.setObject(3, “red”);
ps.setInt(2, 8);
ps.setString(1, “ball”);
ps.executeUpdate();
}
A) INSERT INTO toys VALUES (?, ?, ?)

6.Which of the options can fill in the blank to make the code compile and
run without error?
var sql = “””
UPDATE habitat SET environment = null
WHERE environment = ? “””;
try (var ps = conn.prepareStatement(sql)) {


ps.executeUpdate();
}
Each correct answer represents a complete solution. Choose all that apply.
A) ps.setString(1, “snow”);
ps.setString(1, “snow”); ps.setString(1, “snow”);

7.Which option can fill in the blanks to make the code compile?
boolean bool = ps._____();
int num = ps._____();
ResultSet rs = ps._____();
A) execute, executeUpdate, executeQuery

8.Suppose that the table names 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:hsqldb:file:zoo”);
var ps = conn.prepareStatement(sql)) {
var result = ps.executeUpdate();
System.out.println(result);
}
}
A) 5

9.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)) {
try (var rs = ps.executeQuery()) {
while (rs.next())
System.out.println(rs.getString(1));
}
}
A) An SQLException is thrown.

10.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:hsqldb:file:zoo”);
var ps = conn.prepareStatement(sql)) {
var result = ps.executeUpdate();
System.out.println(result);
}
}
A) The code does not compile.

11.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));
}
try (var rs = ps.executeQuery()) {
while (rs.next())
System.out.println(rs.getObject(1));
}
}
A) 4

12.There are currently 100 rows in the table species before inserting
a new row. What is the output of the following code?
String insert = “INSERT INTO species VALUES (3, ‘Ant’, .05)”;
String select = “SELECT count(*) FROM species”;
try (var ps = conn.prepareStatement(insert)) {
ps.executeUpdate();
}
try (var ps = conn.prepareStatement(select)) {
var rs = ps.executeQuery();
System.out.println(rs.getInt(1));
}
A) A SQLException is thrown.

13.Which of the following options can fill in the blank to make the
code compile and run without error?
var sql = “UPDATE habitat WHERE environment = ?”;
try (var ps = conn.prepareCall(sql)) {


ps.executeUpdate();
}
A) The code throws an exception at runtime.

14.Which of the following can fill in the blank correctly?
var rs = ps.executeQuery();
if (rs.next())
_____________________;
Each correct answer represents a complete solution. Choose all that apply.
A) String s = rs.getString(1)
Object s = rs.getObject(1)

15.Suppose learn() is a stored procedure that takes one IN parameter.
What is wrong with the following code?
var sql = “call learn()”;

try (var cs = conn.prepareCall(sql)) {

cs.setString(1, “java”);

try (var rs = cs.executeQuery()) {

while (rs.next())

System.out.println(rs.getString(3));

}
}
Each correct answer represents a complete solution. Choose all that apply.
A) Line 1 is missing braces.
Line 1 is missing a ?.

16.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,
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)) {
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) 2

17.Suppose learn() is a stored procedure that takes one IN
parameter and one OUT parameter. What is wrong with the following
code?
var sql = “{?= call learn(?)}”;
try (var cs = conn.prepareCall(sql)) {
cs.setInt(1, 8);
cs.execute();
System.out.println(cs.getInt(1));
. }
A) The parameter is not registered for output.

18.Suppose there are two rows in the table before this code is run,
and executeUpdate() runs without error. How many rows are in the
table after the code completes?
conn.setAutoCommit(true);
String sql = “INSERT INTO games VALUES(3, Jenga);”;
try (PreparedStatement ps = conn.prepareStatement(sql,
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
) {
ps.executeUpdate();
}
conn.rollback();
A) Three

19.Which can fill in the blank and have the code run without error?
conn.setAutoCommit(false);
var larry = conn.setSavepoint();
var curly = conn.setSavepoint();
var moe = conn.setSavepoint();
var shemp = conn.setSavepoint();
___________________;
conn.rollback(curly);
Each correct answer represents a complete solution. Choose all that apply.
A) conn.rollback(moe)
conn.rollback(shemp)

20.Suppose conn is a valid connection object and the exhibits table is
empty. Which are true?
try (conn) {
conn.setAutoCommit(false);
String sql = “INSERT INTO exhibits VALUES(3, ‘Test’, 2)”;
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.executeUpdate();
}
conn.setAutoCommit(true); // line W
}
Each correct answer represents a complete solution. Choose all that apply.
A) As written, the table will contain one row after this code.
When line W is commented out, the table will remain empty after this
Code.

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) ResultSet, CallableStatement, Connection

Other Links:



Statistics Quiz




Networking Quiz




See other websites for quiz:



Check on QUIZLET




Check on CHEGG

Leave a Reply

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