Spring MCQs – Multiple Choice Questions and Answers – Part 5
Multiple choice questions and answers (MCQs) on Spring to prepare for exams, tests, and certifications. These questions are taken from a real written exam and some parts are taken from an interview. So you will find questions on basic techniques such as Spring Data, JPA, Spring MVC, XML, Hibernate, and more. This quiz will easily prepare anyone to pass their online test.
1. What is not true about the @PostConstruct, @Resource and @PreDestroy annotations?
A These annotations are specified in the JSR-250
B The context:component-scan
tag enables these annotations
C The Spring framework integrates these annotations
D The contexte:annotation-config
tag enables these annotations
2. Based on the following Spring configuration file, what are the correct instructions?
<bean class="com.spring.service.CompanyServiceImpl" p:CompanyName="StackHowTo"> </bean>
A The namespace p must be declared
B The ID of the bean is CompanyServiceImpl
C CompanyServiceImpl refers to a bean called StackHowTo
D None of the above
3. Based on the following Spring configuration file, what are the correct instructions?
<bean class="com.spring.service.MyServiceImpl"> <property name="repository" ref="jpaDao"/> </bean> <bean class="com.spring.repository.JpaDao"/>
A In the first bean declared on MyServiceImpl, an identifier named myService is missing.
B The second bean declared JpaDao, it is missing an identifier must be named jpaDao
C Both A and B are true.
D Both A and B are false.
4. What is usually the case(s) where you usually need to manually instantiate an ApplicationContext?
A In a web application
B In an integration test executed with SpringJUnit4ClassRunner
C In a standalone application started with the main() method
D None of the above
5. What is the name of the bean defined in the following configuration class. Select only one answer?
@Configuration public class AppConfig { @Autowired private DataSource ds; @Bean ClientRepository clientRepository() { ClientRepository cmpRepository = new JpaClientRepository(); cmpRepository.setDataSource(ds); return cmpRepository; } }
A JpaClientRepository
B clientRepository
C Two beans are defined: data souce and repository
D None of the above
6. What is the scope of a stateless bean in Spring?
A Singleton scope
B Prototype scope
7. How to auto-inject in an attribute a bean by its name?
A With the name attribute of the @Autowired annotation
B Using the unique @Qualifier annotation
C Using the @Autowired and @Qualifier spring annotations
D None of the above
8. What are the main advantages of using Spring when writing unit tests?
A Reuse the Spring configuration files of the application
B Using dependency injection
C Provide mocks for servlet classes
D All the answers are true
9. Select the correct statement regarding the transactional management of the Spring?
A The transaction manager can be defined with the @TransactionConfiguration annotation
B The method with the @Before annotation is executed outside the test transaction
C The Spring framework test can cancel the transaction of a service configured with the REQUIRES_NEW propagation
D All the answers are true
10. Select the correct statement regarding the integration test development with Spring?
A A new Spring context is created for each test class
B To get a reference to the bean you want to test, you have to call the getBean() method of the Spring context.
C The Spring context configuration could inherit from the class super
D The Spring context configuration file must be provided in the parameters of the @ContextConfiguration annotation.