Java OCA OCP Practice Question 2148

Question

Suppose the ResultSet is scrollable and contains 10 rows with values numbered from 1 to 10 in ascending order.

What is true about the following? (Choose two.).

12:  rs.beforeFirst(); 
13:  System.out.println(rs.relative(5)); 
14:  System.out.println(rs.getInt(1)); 
15:  System.out.println(rs.relative(-10)); 
16:  System.out.println(rs.getInt(1)); 
17:  System.out.println(rs.relative(5)); 
18:  System.out.print(rs.getInt(1)); 
  • A. It outputs true once.
  • B. It outputs true twice.
  • C. It outputs true three times.
  • D. It completes without throwing an exception.
  • E. It throws an exception at runtime.


A, E.

Note

Line 12 has no effect.

The cursor starts out positioned immediately before the first row and beforeFirst() keeps it there.

Line 13 moves the cursor to point to row five and prints true.

Line 14 prints the value in that row, which is 5.

Line 15 tries to subtract 10 rows from the current position.

That would be at row negative five.

However, the cursor can't go back further than the beginning, so it stays at row zero.

It also prints false since there isn't data at row zero.

Note this is the same position the cursor was at on line 12.

Line 16 tries to print a value on row zero, but there is no data.

It instead throws a SQLException, making Option E correct.

Option A is also correct since true was only output once.




PreviousNext

Related