Java OCA OCP Practice Question 2256

Question

What is true of the following if the music database exists and contains a songs table with one row when run using a JDBC 4.0 driver? (Choose two.)

import java.sql.*; 
public class Main { 
   public static void main(String[] args) throws Exception { 
      String url = "jdbc:derby:music"; 
      Connection conn = DriverManager.getConnection(url); 
      Statement stmt = conn.createStatement(); 
      stmt.execute("update songs set name = 'The New Song'"); 
   } 
} 
  • A. The code does not compile.
  • B. The code does not update the database because it calls execute() rather than executeUpdate().
  • C. The code does not update the database because the Statement is never closed.
  • D. The code runs without error.
  • E. The execute() method returns a boolean.
  • F. The execute() method returns an int.


D, E.

Note

While this code does not close the Statement and Connection, it does compile, making Option A incorrect.

Java defaults to auto-commit, which means the update happens right away, making Option C incorrect.

Option B is incorrect because either execute() or executeUpdate() is allowed for UPDATE SQL.

The difference is the return type.

The execute() method returns a boolean while the executeUpdate() method returns an int.

The code also runs without error, making Options D and E the answer.

And remember to always close your resources in real code to avoid a resource leak.




PreviousNext

Related