top of page

1. Which of the following annotations can be used on top of a field in Spring, in order for the field to be a candidate for dependency injection?

Explanation:

  • @Autowired:
    Correct, @Autowired annotation in Spring is used for automatic dependency injection, making the annotated field a candidate for injection.

  • @Bean:
    Incorrect, @Bean annotation is used to declare a Spring Bean at the method level, not for field-level dependency injection.

  • @Resource:
    Correct, @Resource annotation, similar to @Autowired, can be used for field-level dependency injection in Spring. However, unlike @Autowired, @Resource follows the JSR-250 standard and performs injection by name rather than type.

  • @Inject:
    Correct, @Inject annotation, part of the JSR-330 standard, can also be used for dependency injection in Spring, making it a valid choice for field-level injection.

     

2. In which of these transaction isolation levels can non-repeatable reads occur?

Explanation:

  • Repeatable Reads:
    Incorrect, this isolation level prevents non-repeatable reads by ensuring that if a transaction reads a row, it can read that row again and get the same values.

  • Read Uncommitted:
    Correct, this is the lowest isolation level and allows non-repeatable reads, as it permits reading uncommitted changes made by other transactions.

  • Serializable:
    Incorrect, this is the highest isolation level and prevents non-repeatable reads by ensuring a transaction can be completed only if no other transactions are modifying data that it has read.

  • Read Committed:
    Correct, this isolation level allows non-repeatable reads because it only guarantees that any data read is committed at the moment it is read; however, it can change before the end of the transaction if other transactions commit changes.

     

3. Which two statements are true regarding bean creation?

Explanation:

​

  • A Spring bean can be implicitly created by annotating the class with @Component and using the component scanner to scan its package:
    This statement is correct. When a class is annotated with @Component (or other stereotype annotations like @Service, @Repository, @Controller), and component scanning is enabled, Spring will automatically detect and create a bean for that class.

  • A Spring bean can be explicitly created by annotating the class with @Autowired:
    This statement is incorrect. The @Autowired annotation is used for dependency injection, not for bean creation. It marks a constructor, field, or setter method to be autowired by Spring's dependency injection facilities.

  • A Spring bean can be explicitly created by annotating methods or fields by @Autowired:
    This statement is incorrect. Similar to the previous statement, @Autowired is used for dependency injection, not for creating beans.

  • A Spring bean can be explicitly created using @Bean annotated methods within a Spring configuration class:
    This statement is correct. The @Bean annotation is used on methods within a @Configuration class to define a bean. The return value of the method is registered as a bean within the Spring ApplicationContext.

  • A Spring bean can be implicitly created by annotating the class with @Bean and using the component scanner to scan its package:
    This statement is incorrect. The @Bean annotation is used on methods, not on classes. It is not used in conjunction with component scanning to create beans.

4. Which two annotations indicate that the translation for a transactional test method should be committed after the test method has completed?

Explanation:

  • @Transactional(commit=true):
    This statement is incorrect. There is no commit attribute in the @Transactional annotation. The correct way to indicate that a transaction should be committed after a test method is with the @Commit annotation.

  • @Commit:
    This statement is correct. The @Commit annotation can be used to indicate that the transaction for a test method should be committed after the test method has completed.

  • @Rollback(false):
    This statement is correct. The @Rollback annotation with the value false can be used to override the default rollback behavior and commit the transaction after a test method has completed.

  • @Sql(alwaysCommit=true):
    his statement is incorrect. The @Sql annotation is used to execute SQL statements against a database, and it does not have an alwaysCommit attribute.

  • @SqlMergeMode(false):
    This statement is incorrect. The @SqlMergeMode annotation is used to specify how @Sql annotations should be combined when used at the class and method levels, and it does not have a boolean attribute.

​

​

​

5. Which two are required to use transactions on Spring?

Explanation:

  • A class returning a transaction must be implemented TransactionInterceptor interface:
    This statement is incorrect. There is no requirement for a class to implement the TransactionInterceptor interface to support transactions in Spring. Transactions are typically managed using the @Transactional annotation.

  • Annotate a class, an interface, or individual methods requiring a transaction with the @Transactional annotation:
    This statement is correct. The @Transactional annotation is used to declare transactional behavior on classes, interfaces, or individual methods in Spring.

  • Add @EnableTransactionManagement to a Java configuration class:
    This statement is correct. The @EnableTransactionManagement annotation is used in a configuration class to enable Spring's annotation-driven transaction management capability.

  • Write a Spring AOP advice to implement transactional behaviour:
    This statement is incorrect. While it is possible to use Spring AOP to implement transactional behavior, it is not a requirement. The @Transactional annotation is the standard way to declare transactional behavior in Spring.

  • A class must be annotated with @Service and @Transaction:
    This statement is incorrect. While the @Service annotation is commonly used to indicate that a class is a service layer component, it is not required for transactional support. The @Transactional annotation is what's used to declare transactional behavior.

6. Which two statements are true regarding storing user details in Spring Security?

Explanation:

  • User details can be stored in custom storage and retrieve them by implementing UserDetailsService interface:
    This statement is correct. The UserDetailsService interface is used in Spring Security to load user-specific data. It can be implemented to retrieve user details from any custom storage, such as a database or a web service.

  • The user details includes username and password but not authorities:
    This statement is incorrect. The UserDetails interface in Spring Security includes not only the username and password but also the authorities (permissions) granted to the user.

  • With a custom UserDetailsService defined in the ApplicationContext, Spring Boot still creates the default user:
    This statement is incorrect. If a custom UserDetailsService is defined, Spring Boot will not create the default user. The custom UserDetailsService will be used to load user details.

  • Passwords must be hashed and the default hashing algorithm is MD5:
    This statement is incorrect. While passwords should be hashed, MD5 is not recommended due to security vulnerabilities. Spring Security recommends using stronger hashing algorithms like BCrypt.

  • User details can be stored in a database, in LDAP, or in memory:
    This statement is correct. Spring Security supports storing user details in various storage mechanisms, including databases, LDAP directories, and in-memory storage.

7. Which two options will inject the value of the daily.limit system property?

Explanation:

  • @Value("${daily.limit}"):
    This statement is correct. The @Value annotation with the "${...}" placeholder syntax is used to inject the value of a property into a field. If the property name matches a system property, such as "daily.limit," its value will be injected.

  • @Value("#{systemProperties['daily.limit']}"):
    This statement is correct. The @Value annotation with Spring Expression Language (SpEL) syntax "#{...}" can be used to access system properties. The expression "#{systemProperties['daily.limit']}" will inject the value of the "daily.limit" system property.

  • @Value("$(systemProperties.daily.limit)"):
    This statement is incorrect. The syntax "$(systemProperties.daily.limit)" is not valid for accessing system properties in Spring. The correct syntax for placeholders is "${...}" and for SpEL is "#{...}".

  • @Value("#(daily.limit)"):
    This statement is incorrect. The syntax "#(daily.limit)" is not valid for accessing properties in Spring. The correct syntax for SpEL expressions is "#{...}".

  • @Value("#(systemProperties.daily.limit)"):
    This statement is incorrect. The syntax "#(systemProperties.daily.limit)" is not valid for accessing system properties in Spring. The correct syntax for SpEL expressions is "#{...}".

8. Which three types can be used as @Controller method arguments?

Explanation:

  • Session:
    This statement is incorrect. The type 'Session' is not a valid argument type for a @Controller method. Instead, the HttpSession type should be used to access session information.

  • HttpSession:
    This statement is correct. HttpSession can be used as a method argument in a @Controller to interact with the HTTP session.

  • Principal:
    This statement is correct. Principal can be used as a method argument in a @Controller to access the currently authenticated user's information.

  • Request:
    This statement is incorrect. The type 'Request' is not a valid argument type for a @Controller method. Instead, HttpServletRequest should be used to access request information.

  • Language:
    This statement is incorrect. 'Language' is not a valid argument type for a @Controller method. To determine the client's preferred language, the Locale type should be used.

  • Locale:
    This statement is correct. Locale can be used as a method argument in a @Controller to determine the client's preferred locale based on the 'Accept-Language' header in the HTTP request.

9. Which dependency enables an automatic restart of the application on code changed during development of a Spring Boot?

Explanation:

  • spring-boot-initializr:
    his statement is incorrect. Spring Boot Initializr is a web-based tool for generating Spring Boot project structures, not a dependency for automatic restarts.

  • spring-boot-starter-devtools:
    his statement is incorrect. The spring-boot-starter-devtools dependency does not exist.

  • spring-boot-restart:
    This statement is incorrect. There is no spring-boot-restart dependency. The functionality for automatic restarts is provided by spring-boot-starter-devtools.

  • spring-boot-devtools:
    This statement is correct. The spring-boot-devtools module includes the automatic restart feature, which is enabled when the module is included in the project.

10. Which three dependencies are provided by the spring-boot-starter-test?

Explanation:

  • EasyMock:
    This statement is incorrect. EasyMock is not included in the spring-boot-starter-test dependency by default.

  • spring-test:
    This statement is correct. The spring-test module is included in the spring-boot-starter-test dependency and provides support for Spring testing features.

  • Cucumber:
    This statement is incorrect. Cucumber is not included in the spring-boot-starter-test dependency by default.

  • PowerMock:
    This statement is incorrect. PowerMock is not included in the spring-boot-starter-test dependency by default.

  • JUnit:
    This statement is correct. JUnit is included in the spring-boot-starter-test dependency, specifically JUnit 5 (Jupiter) for Spring Boot 2.x.

  • Hamcrest:
    This statement is correct. Hamcrest is included in the spring-boot-starter-test dependency and provides a library of matcher objects for building test expressions.

You've Completed the Sample Questions

Well done on reaching the end of the sample questions for the Spring exam! What you've seen is just a preview – our database contains an extensive collection of 970 questions designed to fully prepare you.

 

Are you ready to take your preparation to the next level? Unlock lifetime access to the complete set of questions now and ensure you're fully equipped for success.

​

For more details and pricing, please visit our course website. Don't miss out on the opportunity to advance your knowledge!

Explore 960+ More Questions

bottom of page