Spring Questions and Answers – Declaring Beans

This set of Java Spring Multiple Choice Questions & Answers (MCQs) focuses on “Declaring Beans”.

1. Declaring Beans using:-
a) Static field
b) Object Properties
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: Beans can be declared from static fields as well as Object properties.

2. Ways to declare bean from a static field?
a) FieldRetrievingFactoryBean
b) util:contant
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: To declare a bean from a static field, you can make use of either the built-in factory bean FieldRetrievingFactoryBean, or the util:constant tag in Spring 2.x.

3. Declaring a bean from a static field requires a built-in factory bean FieldRetrievingFactoryBean and fully qualified field name or instance field is specified in the list property.
a) True
b) False
View Answer

Answer: b
Explanation: StaticField property is used to specify instance field name.
advertisement
advertisement

4. A)

      public abstract class Product {
	public static final Product AAA = new Battery("AAA", 2.5);
	public static final Product CDRW = new Disc("CD-RW", 1.5);
	...
      }
      <beans ...>
	<bean id="aaa" class="org.springframework.beans.factory.config.
	FieldRetrievingFactoryBean">
	<property name="staticField">
	<value>com.shop.Product.AAA</value>
	</property>
	</bean>
	<bean id="cdrw" class="org.springframework.beans.factory.config.
	FieldRetrievingFactoryBean">
	<property> name="staticField"
	valuecom.shop.Product.CDRW/value
	</property>
	</bean>
     </beans>
 
   B) Product aaa = com.shop.Product.AAA;
      Product cdrw = com.shop.Product.CDRW;

a) A and B are equivalent
b) A and B provides different functionality
c) Runtime Error in A
d) Exception in B
View Answer

Answer: a
Explanation: StaticField method fills up the static fields with the provided property values.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

5. As an alternative to specifying the field name in the staticField property explicitly, you can set it as the bean name of FieldRetrievingFactoryBean.
a) True
b) False
View Answer

Answer: a
Explanation: Bean name may get rather long and verbose.
advertisement

6. Is this bean configuration metadata correct?

   <beans ...>
	bean id="com.shop.Product.AAA"
	class="org.springframework.beans.factory.config.
	FieldRetrievingFactoryBean" /
	bean id="com.shop.Product.CDRW"
	class="org.springframework.beans.factory.config.
	FieldRetrievingFactoryBean" /
   </beans>

a) Yes
b) No
View Answer

Answer: a
Explanation: This is an alternate method of using staticField in property attributes.
advertisement

7. Which tag is also allowed by static field?
a) util:constant
b) list
c) set
d) constructor-args
View Answer

Answer: a
Explanation: Spring 2 and later allow you to declare a bean from a static field by using the util:constant tag.

8. Is this bean configuration correct?

    <beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/util/spring-util-3.0.xsd"
	util:constant id="aaa"
	static-field="com.shop.Product.AAA" />
	util:constant id="cdrw"
	static-field="com.shop.Product.CDRW" />
  </beans>

a) Yes
b) No
View Answer

Answer: b
Explanation: Schema for util:constant needs to be included using this link: http://www.springframework.org/schema/util.

9. Declaring bean form object properties can be done using:-
a) PropertyPathFactoryBean
b) util:constant
c) None of the mentioned
d) All of the mentioned
View Answer

Answer: a
Explanation: To declare a bean from an object property or a property path, you can make use of either the built-in factory bean PropertyPathFactoryBean or the util:property-path tag in Spring 2.x.

10. Inner Bean can be retrieved by it’s name.
a) True
b) False
View Answer

Answer: b
Explanation: Since inner beans are local to another bean, so can’t be accessed globally.

11. PropertyPathFactoryBean declares a bean from an:-
a) Object Property
b) Property Path
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: The factory bean PropertyPathFactoryBean can be used to declare a bean from an object property or a property path.

12. The propertyPath property of PropertyPathFactoryBean can accept only a single property name.
a) True
b) False
View Answer

Answer: b
Explanation: The propertyPath property of PropertyPathFactoryBean can accept not only a single property name but also a property path with dots as the separators.

13. Alternate way of PropertyPathFactoryBean to declare a bean.
a) util:property-path tag
b) util:constant tag
c) None of the mentioned
d) All of the mentioned
View Answer

Answer: a
Explanation: Compared to using PropertyPathFactoryBean, it is a simpler way of declaring beans from properties.

14. We can combine target Object and propertyPath properties as bean name/id of PropertyPathFactoryBean.
a) True
b) False
View Answer

Answer: a
Explanation: In addition to specifying the targetObject and propertyPath properties explicitly, you can combine them as the bean name of PropertyPathFactoryBean. The downside is that your bean name may get rather long and verbose.

15. The Spring Expression Language can be accessed by:-
a) XML configuration
b) Annotations
c) None of the mentioned
d) All of the mentioned
View Answer

Answer: d
Explanation: The expression language is available via XML or annotations.

Sanfoundry Global Education & Learning Series – Java Spring.
To practice all areas of Java Spring, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.