Java OCA OCP Practice Question 2049

Question

Which is true if the myTable database exists and contains an empty myTable table?.

String url = "jdbc:derby:myTable"; 
try (Connection conn = DriverManager.getConnection(url); 
    Statement stmt = conn.createStatement(); 
    ResultSet rs = stmt.executeQuery("select count(*) from myTable")) { 

   rs.next();   // r1 
   System.out.println(rs.getInt(1));   // r2 
} 
  • A. The code compiles and runs without error.
  • B. The code does not compile.
  • C. The code compiles but throws an exception at runtime on line r1.
  • D. The code compiles but throws an exception at runtime on line r2.


A.

Note

The count(*) function in SQL always returns a number.

In this case, it is the number zero.

This means line r1 executes successfully because it positions the cursor at that row.

Line r2 also executes successfully and prints 0, which is the value in the row.

Since the code runs successfully, Option A is the answer.




PreviousNext

Related