Java OCA OCP Practice Question 2266

Question

What is true about the following code? (Choose two.)

public static void main(String[] args) throws Exception { 
   String url = "jdbc:derby:hats;create=true"; 
   Connection conn = null; /*from   w  w  w .  j  a v  a 2  s .  co  m*/
   Statement stmt = null; 


   try { 
     conn = DriverManager.getConnection(url); 
     stmt = conn.createStatement(); 
     stmt.executeUpdate( 
       "CREATE TABLE caps (name varchar(255), size varchar(1))"); 
   } finally { 
       conn.close(); 
       stmt.close(); 
   } 
} 
  • A. If using a JDBC 3.0 driver, this code throws an exception.
  • B. If using a JDBC 4.0 driver, this code throws an exception.
  • C. The resources are closed in the wrong order.
  • D. The resources are closed in the right order.
  • E. The Connection is created incorrectly.
  • F. The Statement is created incorrectly.


A, C.

Note

JDBC 3.0 drivers require a Class.forName() call.

Since this is missing, Option A is correct, and Option B is incorrect.

The Connection and Statement creation are correct, making Options E and F incorrect.

Since the call to stmt.close() should be before the call to conn.close(), Option C is correct, and Option D is incorrect.




PreviousNext

Related