Java OCA OCP Practice Question 2029

Question

Assuming the myDB database exists and contains one empty table named myDB,

what is the output of the following when run using a JDBC 4.0 driver?

import java.sql.*; 

public class Main { 
  public static void main(String[] args) throws SQLException {  // s1 
     String url = "jdbc:derby:myDB"; 
     try (Connection conn = new Connection(url);   // s2 
        Statement stmt = conn.createStatement(); 
        ResultSet rs = stmt.executeQuery("select * from myDB")) { 


        if (rs.next()) 
           System.out.println(rs.getString(1)); 
     } //w w w.jav  a 2s  .c o  m
  } 
} 
  • A. The code terminates successfully without any output.
  • B. The code does not compile due to line s1.
  • C. The code does not compile due to line s2.
  • D. None of the above


C.

Note

Connection is an interface rather than a concrete class.

Therefore, it does not have a constructor and line s2 does not compile.

Option C is the answer.

Option A would be the answer if the code new Connection() was changed to DriverManager.getConnection().




PreviousNext

Related