MCQ

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

C

 

 

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

A

 

 
 

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.

B
These beans are anonymous because no identifier is explicitly provided. Thus, the Spring container generates a unique identifier for this bean. It uses the full class name and adds a number to them. However, if you want to refer to this bean by its name, you have to provide a name through the use of the ref element. To be accurate, the second bean has to declare an id jpaDao attribute in order to be referenced by the repository property of the first bean.

 

 

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

C
  • In a Web application, ContextLoaderListener is responsible for creating a WebApplicationContext.
  • In a Spring-based integration test, SpringJUnit4ClassRunner creates the application context for you. The @ContextConfiguration annotation is used to specify application context configuration files.
  • In the main method, you must instantiate a class implementing the ApplicationContext interface (examples: ClassPathXmlApplicationContext or FileSystemXmlApplicationContext).

 

 

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

B
The @Bean annotation defines a String bean with the clientRepository id. JpaClientRepository is the implementation class of the bean. Data source is injected and is not declared in this class.

 

 
 

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

A Singleton scope

B Prototype scope

A
Prototype scope. 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. 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

C
Example:

public class ClientBean{
	
	@Qualifier("bean1")
	@Autowired
	private MonBean monBean;
	
	...
	
}

 

 

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

C
What are the main advantages of using Spring when writing unit tests?

  • You do not need the Spring container to write the unit test.
  • The org.springframework.mock package provides mock classes such as MockHttpSession or MockHttpContext. They could be useful for unit testing in the presentation layer and when you are not using a mock framework such as Mockity or EasyMock.

 

 

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

A
TransactionConfiguration defines class-level metadata for transactional test configuration.

 

 
 

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.

C
  • The Spring context is cached in the tests, unless you use the @DirtiesContext annotation.
  • With the Spring test module, dependency injection is available when testing. So you can automatically inject the bean to test it
  • By default, a class with the @ContextConfiguration annotation inherits the context configuration file locations defined by the annotation of a superclass. InheritLocations of this attribute allows to change this default behavior.
  • If no context configuration file is provided to the @ContextConfiguration annotation, Spring uses a convention name. It tries to load a file named with the name of the test class with the suffix “-context.xml” (i.e. MyTestClass.xml).

 

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 *