250+ TOP MCQs on Declaring Beans and Answers

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

1. Declaring Beans using:-
a) Static field
b) Object Properties
c) All of the mentioned
d) None of the mentioned
Answer: c
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: StaticField property is used to specify instance field name.

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.AAAvalue>
	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
Answer: a
Clarification: StaticField method fills up the static fields with the provided property values.

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
Answer: a
Clarification: Bean name may get rather long and verbose.

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
Answer: a
Clarification: This is an alternate method of using staticField in property attributes.

7. Which tag is also allowed by static field?
a) util:constant
b) list
c) set
d) constructor-args
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: d
Clarification: The expression language is available via XML or annotations.

Leave a Reply

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