250+ TOP MCQs on Using the Simple JDBC Template and Handling Exceptions and Answers

Java Spring Quiz on “Using the Simple JDBC Template and Handling Exceptions”.

1. JdbcTemplate that takes advantage of Java 1.5 features such as autoboxing, generics, and variable-length arguments to simplify its usage.
a) org.springframework.jdbc.core.JdbcTemplate
b) org.springframework.jdbc.core.simple.SimpleJdbcTemplate
c) org.springframework.jdbc.*
d) none of the mentioned
Answer: b
Clarification: org.springframework.jdbc.core.simple.SimpleJdbcTemplate is an evolution of JdbcTemplate that takes advantage of Java 1.5 features such as autoboxing, generics, and variable-length arguments to simplify its usage.

2. JdbcTemplate require statement parameters to be passed as an object array.
a) True
b) False
Answer: a
Clarification: In SimpleJdbcTemplate, they can be passed as variable-length arguments; this saves you the trouble of wrapping them in an array.

3. To use SimpleJdbcTemplate:-
a) instantiate it directly
b) retrieve its instance by extending the SimpleJdbcDaoSupport class
c) all of the mentioned
d) none of the mentioned
Answer: c
Clarification: To use SimpleJdbcTemplate, you can either instantiate it directly or retrieve its instance by extending the SimpleJdbcDaoSupport class.

4. SimpleJdbcTemplate offers a convenient batch update method in the form of:-
a) Vector
b) Set
c) Map
d) List
Answer: d
Clarification: SimpleJdbcTemplate offers a convenient batch update method for you to specify a SQL statement and a batch of parameters in the form of List

5. Method has a warning from the Java compiler because of an unchecked conversion from List to List.
a) findAll()
b) query()
c) update()
d) batchUpdate()
View Answer

Answer: a
Clarification: When using the classic JdbcTemplate, the findAll() method has a warning from the Java compiler because of an unchecked conversion from List to List. This is because the return type of the query() method is List rather than the type-safe List.

6. The return type of the queryForObject() method will be determined by the class argument (e.g., String.class).
a) True
b) False
Answer: a
Clarification: So, there’s no need for you to perform typecasting manually.

7. Named SQL parameters are specified by name (starting with a colon) rather than by position.
a) True
b) False
Answer: a
Clarification: Another option when binding SQL parameters in the Spring JDBC framework is to use named parameters.

8. Named parameters are supported only in SimpleJdbcTemplate.
a) True
b) False
Answer: a
Clarification: Named parameters are supported only in SimpleJdbcTemplate and NamedParameterJdbcTemplate.

9. Implementations of the SqlParameterSource interface:-
a) MapSqlParameterSource
b) BeanPropertySqlParameterSource
c) none of the mentioned
d) all of the mentioned
Answer: d
Clarification: There are three implementations of the SqlParameterSource interface. The basic one is MapSqlParameterSource, which wraps a map as its parameter source.

10. The Spring framework offers a consistent data access exception-handling mechanism for its data access module.
a) True
b) False
Answer: a
Clarification: In general, all exceptions thrown by the Spring JDBC framework are subclasses of org.springframework.dao.DataAccessException, a type of RuntimeException that you are not forced to catch.

11. In your DAO methods, you neither need to surround the code with a try/catch block nor declare throwing an exception in the method signature.
a) True
b) False
Answer: a
Clarification: This is because DataAccessException (and therefore its subclasses, including DuplicateKeyException) is an unchecked exception that you are not forced to catch.

12. The direct parent class of DataAccessException is:-
a) RuntimeException
b) NestedRuntimeException
c) Exception
d) Throwable
Answer: b
Clarification: A core Spring exception class that wraps another exception in a RuntimeException.

13. Which concrete exception in the DataAccessException hierarchy should be thrown?
a) errorCode
b) SQLState properties of the caught SQLException
c) All of the mentioned
d) None of the mentioned
Answer: c
Clarification: As a DataAccessException wraps the underlying SQLException as the root cause, you can inspect the errorCode and SQLState properties with the following catch block.

14. Error code for The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint.
a) -1
b) 0
c) 23505
d) 1
Answer: a
Clarification: SQL State Message Text
23505 The statement was aborted because it would have caused a duplicate key value in a unique or primary.

15. How does the Spring JDBC framework know that state 23505 should be mapped to DuplicateKeyException?
a) error code
b) SQL state
c) all of the mentioned
d) none of the mentioned
Answer: c
Clarification: The error code and SQL state are database specific, which means different database products may return different codes for the same kind of error.


Java Spring for quizzes,

Leave a Reply

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