Java OCA OCP Practice Question 2027

Question

What is the output when run with a JDBC 4.0 driver if the clowns database exists and contains an empty clowns table?

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


    System.out.println(rs.getInt(1)); 
} 
  • A. 0
  • B. 1
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


D.

Note

This code is missing a call to rs.next().

As a result, rs.getInt(1) throws a SQLException with the message Invalid cursor state - no current row.

Therefore, Option D is the answer.




PreviousNext

Related