MCQ

Spring MCQs – Multiple Choice Questions and Answers – Part 6

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. Which of the following statements is correct about the Spring Framework

A The Spring Framework is a heavy-weight solution.

B The Spring Framework is a light-weight solution.

C Both A and B are true.

D None of the above

B
There are many reasons why spring is a lightweight framework.

  • Spring provides you with different modules and allows you to use the one that suits you best. Ideally, the spring JAR file is only 2 to 3 MB in size.
  • If you compare Spring with EJB, you have to write much less code and configurations. The beauty of Spring is that you can focus on the business logic whereas in EJB, you have to write a lot of code along with the business logic which makes it bulky and tightly coupled.
  • With Spring, you play with POJO which does not depend on a Framework and improves the testability of your code.
  • Spring offers seamless integration with frameworks, third-party libraries, etc.

 

 

2. In which scope can you create any number of bean instances?

A Request scope

B Prototype scope

B
The Prototype scope extends a bean definition to an unlimited number of object instances.

 

 
 

3. When a bean has a limited scope to an HTTP request, it is called _____?

A Request scope

B Session scope

A
The Request scope extends a single bean definition to a single HTTP request life cycle; that is, each HTTP request will have its own instance of a created bean. Only valid in the context of ApplicationContext.

 

 

4. When a bean has a limited scope to an HTTP session, it is called _____?

A Request scope

B Session scope

B
Session scope extends a single bean definition to a single HTTP session lifecycle. Valid only in the context of ApplicationContext in Spring.

 

 

5. In which scope a single instance of a bean is created by the IoC container?

A Singleton scope

B Request scope

A
The Singleton scope extends a definition of a bean to a single instance of an object in each IoC container.

 

 
 

6. What is the scope of a stateless bean in Spring?

A Singleton scope

B Prototype scope

A
If the scope is set to prototype, the Spring IoC container creates a new instance of the bean object each time a request for that specific bean is made. As a general rule, use the prototype scope for all state-full beans and the singleton scope for stateless beans.

 

 

7. What is the correct answer about the bean life cycle in Spring.

A The method with the @PostConstruct annotation is called after the instantiation of the bean and before the setting of its properties.

B The @PreDestroy method of a bean prototype is called when the bean is destroyed.

C The init() method declared in the init-method attribute of a bean is called before the afterPropertiesSet callback method of the InitializingBean interface.

D The method with the @PostConstruct annotation is called before the afterPropertiesSet callback method of the InitializingBean interface.

D
  • In the life cycle of a bean, the method with the @PostConstruct annotation is called after the property definition step and the BeanPostProcessors#postProcessBeforeInitialization step.
  • Destroy prototype methods of a bean are never called
  • In the life cycle of a bean, the callback method afterPropertiesSet of InitializingBean is called after the method with the @PostConstruct annotation and before the init method declared in the XML configuration file.
  • In the life cycle of a bean, the method with the @PreDestroy annotation is called before the destroy callback of the DisposableBean interface and before the destroy method declared in the XML configuration file.

 

 

8. Which one is not correct about the benefits of using Spring when writing integration tests?

A Reuse the Spring configuration files of the application

B Create a mock or a stub

C Be able to use the restoration after the test

D Using dependency injection

B
Mocks or stubs are more common in unit tests than in integration tests. And Spring does not provide any implementation or abstraction of the mock.

 

 

9. What is the correct answer about the Spring test module?

A It provides an abstraction layer for open source mock frameworks

B Provides the @Mock annotation

C It dynamically generates mock objects

D None of the above.

D

 

 
 

10. What are the features of XML <contexte: namespace ?

A Analysis of transactional annotations

B Enable @Aspect annotation detection

C Enable @Autowired annotation

D None of the above

C
  1. Use <tx:annotation-driven> to activate the @Transactional annotation
  2. Use <aop: aspectj-autoproxy> to enable detection of the @Aspect bean.
  3. Use <contexte: annotation-config> or <contexte: composant-scan> to activate the @Autowiring annotation

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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