Java OCA OCP Practice Question 2055

Question

What is the most likely outcome of this code if the people table is empty?.

6:  Statement stmt = conn.createStatement(); 
7:  ResultSet rs1 = stmt.executeQuery("select * from people"); 
8:  ResultSet rs2 = stmt.executeQuery("select * from people"); 
9:  System.out.println(rs1.next() + " " + rs2.next()); 
  • A. It prints false false.
  • B. It prints true false.
  • C. It does not terminate.
  • D. It throws a SQLException.


D.

Note

When running a query on a Statement, Java closes any already open ResultSet objects.

This means that rs1 is closed on line 8.

Therefore, it throws a SQLException on line 9 because we are trying to call next() on a closed ResultSet, and Option D is correct.




PreviousNext

Related