Java OCA OCP Practice Question 2062

Question

Given the following code snippet and the table below, what is the output 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 * from people order by last_name asc")) { 


   rs.afterLast(); /*from   w w  w  .jav a  2 s.  co m*/
   rs.next(); 
   rs.next(); 
   rs.previous(); 
   rs.previous(); 
   System.out.println(rs.getString(1)); 
} 

rst_name                last_name 
character varying(255)   character varying(255) 
Java                   Server 
SQL                  Database 
HTML                    Makup 
Javascript                    Client 
  • A. SQL
  • B. HTML
  • C. Javascript
  • D. The code throws a SQLException at runtime.


B.

Note

Since the ResultSet type allows scrolling, the code does not throw a SQLException at runtime.

Immediately after getting the ResultSet, the cursor is positioned at the end immediately after Javascript's row.

The next two lines try to move forward one row.

This has no effect since the cursor is already at the end.

Then previous() moves the cursor to point to the last row, which is Javascript's row.

The second previous() call moves the cursor up one more row to point to HTML's row, making Option B the answer.




PreviousNext

Related