Java OCA OCP Practice Question 2043

Question

Given that the people table has 10 rows, what is the result of the following when using a driver that supports a scroll sensitive ResultSet?.

try (Connection conn = DriverManager.getConnection(url); 
   Statement stmt = conn.createStatement( 
      ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); 
   ResultSet rs = stmt.executeQuery("select count(*) from people")) { 

   rs.next(); 
   rs.absolute(0);                    // q1 
   System.out.print(rs.getInt(1));    // q2 
} 
  • A. 10
  • B. The code does not compile.
  • C. Line q1 throws a SQLException.
  • D. Line q2 throws a SQLException.


D.

Note

rs.next() moves the cursor to point to the first row, which contains the number 10.

Line q1 moves the cursor to immediately before the first row.

This is the same as the position it was in before calling rs.next() in the first place.

It is a valid position but isn't a row of data.

Line q2 tries to retrieve the data at this position and throws a SQLException because there isn't any data, making Option D the answer.




PreviousNext

Related