nerdexam
Oracle

1Z0-809 · Question #98

Given: ITEM ID, INTEGER: PK DESCRIP, VARCHAR (100) PRICE, REAL QUANTITY < INTEGER And given the code fragment: 9. try { 10. Connection conn = DriverManager.getConnection(dbURL, username, password)…

The correct answer is D. The code prints information about item 110. Option D is correct because all three preconditions (driver in classpath, valid database credentials, valid SQL) are explicitly guaranteed, meaning the connection succeeds, the query executes, and the while(rs.next()) loop retrieves the row for item 110 and prints each field…

Question

Given: ITEM ID, INTEGER: PK DESCRIP, VARCHAR (100) PRICE, REAL QUANTITY < INTEGER And given the code fragment: 9. try { 10. Connection conn = DriverManager.getConnection(dbURL, username, password); 11. String query = "Select * FROM Item WHERE ID = 110"; 12. Statement stmt = conn.createStatement(); 13. ResultSet rs = stmt.executeQuery(query); 14. while(rs.next()) { 15. System.out.println("ID:" + rs.getInt("ID")); 16. System.out.println("Description:" + rs.getString("Descrip")); 17. System.out.println("Price:" + rs.getDouble("Price")); 18. System.out.println("Quantity:" + rs.getInt("Quantity")); 19. } 20. } catch (SQLException se) { 21. System.out.println("Error"); 22. } Assume that:
  • The required database driver is configured in the classpath.
  • The appropriate database is accessible with the dbURL, username, and password exists.
  • The SQL query is valid. What is the result?

Options

  • AAn exception is thrown at runtime.
  • BCompilation fails.
  • CThe code prints Error.
  • DThe code prints information about item 110.

How the community answered

(34 responses)
  • A
    6% (2)
  • B
    12% (4)
  • C
    3% (1)
  • D
    79% (27)

Explanation

Option D is correct because all three preconditions (driver in classpath, valid database credentials, valid SQL) are explicitly guaranteed, meaning the connection succeeds, the query executes, and the while(rs.next()) loop retrieves the row for item 110 and prints each field using the appropriate JDBC getter methods (getInt, getString, getDouble).

Option A is wrong because runtime exceptions (specifically SQLException) are only thrown when something goes wrong - a bad connection, invalid query, or missing driver - all of which the problem explicitly rules out. Option B is wrong because the code is syntactically valid Java; the try/catch, ResultSet loop, and JDBC API calls all compile correctly. Option C is wrong because "Error" only prints if a SQLException is caught in the catch block, and since the preconditions guarantee a clean execution path, that block is never reached.

Memory tip: When a JDBC question provides "assume everything works" guarantees, treat the catch block as dead code - the only way to see "Error" is if a SQLException is actually thrown, which the problem has already forbidden.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice