Top MCQs on JDBC (Advanced Java)

11.) Which type of statement is suitable for executing precompiled SQL queries with parameters?

A) Statement
B) PreparedStatement
C) CallableStatement
D) ResultStatement

Answer: Option B

Explanation: A PreparedStatement is suitable for executing precompiled SQL queries with parameters, which can improve performance and security.

12.) Which method is used to execute an SQL query using a JDBC Statement?

A) executeQuery()
B) executeUpdate()
C) execute()
D) All of these

Answer: Option D

Explanation: In Java JDBC, executeQuery() retrieves data from a database, executeUpdate() modifies data, and execute() executes any SQL statement.

13.) In JDBC, what is a ResultSet?

A) A connection to the database
B) A precompiled SQL statement
C) A collection of retrieved data rows from a query
D) A JDBC driver class

Answer: Option C

Explanation: A ResultSet is a collection of retrieved data rows from a database query.

14.) How do you iterate through the rows of a ResultSet in JDBC?

A) Using a for loop
B) Using a while loop
C) Using a do-while loop
D) By calling the next() method

Answer: Option D

Explanation: You iterate through the rows of a ResultSet by calling the next() method, which moves the cursor to the next row.

15.) Which method is used to execute an SQL statement that inserts, updates, or deletes data in the database?

A) executeQuery()
B) executeUpdate()
C) executeSqlQuery()
D) executeStatement()

Answer: Option B

Explanation: The executeUpdate() method is used to execute an SQL statement that inserts, updates, or deletes data in the database.

16.) What is the return value of the executeUpdate() method in JDBC?

A) The number of rows affected by the SQL statement
B) The SQL query string
C) The result set of the query
D) A boolean indicating success or failure

Answer: Option A

Explanation: The executeUpdate() method returns the number of rows affected by the SQL statement.

17.) To prevent SQL injection attacks, which type of statement should you use when executing SQL statements with user-provided input?

A) Statement
B) PreparedStatement
C) CallableStatement
D) ResultStatement

Answer: Option B

Explanation: To prevent SQL injection attacks, it is recommended to use PreparedStatement when executing SQL statements with user-provided input because it handles parameterization and sanitation.

18.) Which exception is commonly thrown when a JDBC connection cannot be established?

A) SQLException
B) ConnectionException
C) DatabaseException
D) JDBCException

Answer: Option A

Explanation: A SQLException is commonly thrown when a JDBC connection cannot be established due to issues such as incorrect credentials or database unavailability.

19.) What is the purpose of handling exceptions in JDBC?

A) To terminate the application if an exception occurs
B) To ignore exceptions and continue execution
C) To log and report errors gracefully
D) To improve query performance

Answer: Option C

Explanation: Handling exceptions in JDBC is essential to log and report errors gracefully, ensuring that the application can respond to issues effectively.

20.) Which method is used to obtain details about a SQLException in JDBC?

A) getErrorMessage()
B) getErrorCode()
C) getExceptionDetails()
D) getErrorDescription()

Answer: Option B

Explanation: The getErrorCode() method is used to obtain the error code associated with a SQLException in JDBC.

Leave a Reply

Your email address will not be published. Required fields are marked *