Java OCA OCP Practice Question 2916

Question

There are currently 100 rows in the table cat before inserting a new row.

What is the output of the following code?

try (Connection conn = DriverManager.getConnection("jdbc:derby:zoo"); 
     Statement stmt = conn.createStatement()) { 

   ResultSet rs = stmt.executeQuery("select count(*) from cat"); 
   int num = stmt.executeUpdate("INSERT INTO cat VALUES (3, 'Ant', .05)"); 
   rs.next(); 
   System.out.println(rs.getInt(1)); 
} 
  • A. 100
  • B. 101
  • C. The code does not compile.
  • D. A SQLException is thrown.
  • E. A different exception is thrown.


D.

Note

A Statement automatically closes the open ResultSet when another SQL statement is run.

This means that rs is no longer open by the println, and a SQLException is thrown because the ResultSet is closed.




PreviousNext

Related