1Z0-819 · Question #140
Given: var sql = "SELECT * FROM students"; try (var conn = DriverManager.getConnection(args[0]); var ps = conn.prepareStatement(sql)) { ResultSet rs = ps._ ____(); while (rs.next()) { int i = ps._…
The correct answer is C. executeQuery, execute, executeUpdate. Option C works because the two typed variable assignments in the code rigidly constrain which methods can fill each blank: ResultSet rs = ps.executeQuery() compiles because executeQuery() is the only PreparedStatement method that returns a ResultSet, and int i =…
Question
Options
- Aexecute, executeUpdate, executeQuery
- BexecuteUpdate, executeQuery, execute
- CexecuteQuery, execute, executeUpdate
- DexecuteUpdate, execute, executeQuery
- Eexecute, executeQuery, executeUpdate
- FexecuteQuery, executeUpdate, execute
How the community answered
(52 responses)- A10% (5)
- B2% (1)
- C85% (44)
- D4% (2)
Explanation
Option C works because the two typed variable assignments in the code rigidly constrain which methods can fill each blank: ResultSet rs = ps.executeQuery() compiles because executeQuery() is the only PreparedStatement method that returns a ResultSet, and int i = ps.executeUpdate() compiles because executeUpdate() returns an int representing the row count. The execute() method in C is the leftover option - it returns boolean and cannot be assigned to either ResultSet or int, confirming it has no valid role here. Options A, B, D, and E are immediately eliminated because they place either execute (returns boolean) or executeUpdate (returns int) as the first method, and neither can be assigned to ResultSet. Option F (executeQuery, executeUpdate, execute) is the common trap - it starts correctly with executeQuery, but the ordering places execute (boolean) as the candidate for blank 2 rather than executeUpdate, making it fail to assign to int i.
Memory tip: Think Q → RS → int - Query gives you a ResultSet; Update gives you an int (rows changed); Execute gives you a boolean (anything else). Match the return type to the variable type, and the blanks fill themselves.
Topics
Community Discussion
No community discussion yet for this question.