MCQ

Spring MCQs – Multiple Choice Questions and Answers – Part 3

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. How to explicitly close the IoC container in non-web applications?

A By using shutdownNow()

B registerShutdownHook()

B
registerShutdownHook() in a standalone (non-Web) application:
The @PreDestroy annotation is used on the bean method to be notified when the bean is removed from the context or when the context is being closed.

The stop event is triggered when context.close() or context.registerShutdownHook() is called.

@Component(value="myBean")
public class BeanExample {

    @PreDestroy
    public void destroy() {
        System.out.println("The bean is removed ...");
    }
}

 

 

2. How to enable annotations in Spring?

A Add <annotation-context: config /> to the bean configuration.

B Add <annotation-config /> to the bean configuration.

C Add <annotation-context-config /> to the bean configuration.

D Add <contexte: annotation-config /> to the bean configuration.

D
Add <contexte: annotation-config /> to the bean configuration to enable the use of annotations.

 

 
 

3. Which ORM does Spring support?

A Hibernate

B iBatis

C JPA

D All the answers are true

E None of the above

D
Spring supports most ORMs, including Hibernate, JDO, TopLink, iBATIS and JPA.

 

 

4. A bean must have an id attribute in the bean configuration file?

A True

B False

B
The id is not a mandatory attribute in the bean configuration file.

 

 

5. The ________ class can be extended to create a custom event in Spring.

A SpringEvent

B Event

C ApplicationEvent

D None of the above

C
ApplicationEvent is used to create custom events.

 

 
 

6. How to use <ref> tag in the Spring framework?

A <ref> tag is used with the bean ID.

B <ref> tag is used with a String value.

C Both A and B are true.

D None of the above

A
In Spring, we must use the <ref> tag to inform the Spring container of the object’s dependency.

In Spring, beans can “access” each other by specifying their references in the same configuration file or in a different configuration file. In Spring, we can write several configuration xml files. Our associated bean can be in the same xml or in another xml file.

Example : person-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>   
    <bean id="personDetails" class="com.jwt.spring.PersonImpl"/>
</beans>

 

Now, in the XML file below, we refer to “personDetails” configured in the person-bean.xml file. We must therefore use the “ref” tag with the “bean” attribute i.e. <ref bean =" personDetails "/>.

company-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>   
    ....
    <bean id="companyId" class="com.jwt.spring.Company">
      <property name="perdetails">
        <ref bean="personDetails"/>
      </property>
    </bean>
     ....
</beans>

 

 

7. How to define a bean in Spring?

A Use only the following <property />

B Use only the following <constructor-arg />

C Use <property /> or <constructor-arg />.

D None of the above

C
Example:

<bean id="objetB" scope="request" class="a.b.c.classeB"/>

<bean id="objetA" scope="request" class="a.b.c.classeA">
    <constructor-arg ref="objetB" />
    <!-- OR -->
    <property name="bRef" ref="objetB" />
</bean>

 

 

8. Which property is replaced by p-namespace in the Spring framewrok?

A <property />

B <constructor-arg />
 

 
A
In spring, p-namespace is an XML shortcut to inject a dependency into the bean. p-namespace replaces the <property> tag of XML. p-namespace has no XSD definition and exists only in the spring core. We can directly assign the attribute name of the class with p-namespace in the bean tag. We can use p-namespace instead of the <property> tag in spring XML. It is easy and clear to use, which will increase the readability of the XML context. Suppose we have the following <bean> definition in XML.

<bean id="per" class="x.y.z.Person">
	<property name="name" value="Alex"/>
	<property name="address" value="California"/>
</bean>

We can change the <property> tag using p-namespace as follows.

<bean id="per" class="x.y.z.Person" p:name="Alex" p:address="California"/>

 

9. Which property is replaced by c-namespace in the Spring framewrok?

A <property />

B <constructor-arg />

B
c-namespace was introduced in spring 3.1. It replaces the old style of constructor-arg. The bean that needs to be configured with c-namespace must have a constructor to accept these arguments.

Example – The old style :

<bean id="std" class="x.y.z.Student">
	<constructor-arg name="name" value="BOB"/>
	<constructor-arg name="age" value="25"/>
</bean>

We can replace constructor-arg using c-namespace as follows.

<bean id="std" class="x.y.z.Student" c:name="BOB" c:age="25"/>

 

 

10. What is the purpose of “ApplicationContextAware” in Spring?

A The dependency injection is performed.

B Makes a bean aware of the container.

B
The Bean implementing the “ApplicationContextAware” interface can get the current context of the application and can be used to call any service from the application context.

 

 
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 *