Java OCA OCP Practice Question 2204

Question

Which of the following can be independently inserted into the blank so the code can run without error for at least one SQL query?

private static void choices(Connection conn, String sql) 
      throws SQLException { 
   try (Statement stmt = conn.createStatement(); 
       ResultSet rs = stmt.executeQuery(sql)) { 


        
   } 
} 
  • I. System.out.println(rs.getInt(1));
  • II. rs.next(); System.out.println(rs.getInt(1));
  • III. if (rs.next()) System.out.println(rs.getInt(1));
  • A. II
  • B. III
  • C. I and III
  • D. II and III
  • E. I, II, and III
  • F. None of the above


D.

Note

The most common approach is III, which works for any SELECT statement that has an int in the first column.

If the SELECT statement has a function like count(*) or sum(*) in the first column, there will always be a row in the ResultSet, so II works as well.

Therefore, Option D is the answer.




PreviousNext

Related