Java OCA OCP Practice Question 2060

Question

Assuming the myTable database exists and contains one empty table named myTable, 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:myTable"; 
     try (Connection conn = DriverManager.getConnection(url);   // s2 
        Statement stmt = conn.createStatement(); 
        ResultSet rs = stmt.executeQuery("select * from myTable")) { 


        if (rs.next()) 
           System.out.println(rs.getString(1)); 
     } //from  w w w  . j ava  2  s .  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


A.

Note

This code correctly obtains a Connection and Statement.

It then runs a query, getting back a ResultSet without any rows.

The rs.next() call returns false, so nothing is printed, making Option A correct.




PreviousNext

Related