Spring Questions and Answers – Creating Beans

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

1. Beans can be created by which of the following properties?
a) Scope
b) Property
c) Class
d) It’s own constructor
View Answer

Answer: d
Explanation: Class’s constructor can create bean.

2. Which attribute is used to specify class name of the bean?
a) name
b) id
c) class
d) constructor-args
View Answer

Answer: c
Explanation: Class attribute is mandatory and denotes the class used to create bean.

3. Which of the following method can be used to used to instantiate a method?
a) static factory method
b) default-init method
c) destroy method
d) lazy-init method
View Answer

Answer: a
Explanation: Class attribute is used to specify the name of the class that contains the static factory method.
advertisement
advertisement

4. Which attribute is used to specify static factory-method?
a) factory-method
b) default-init method
c) destroy method
d) lazy-init method
View Answer

Answer: a
Explanation: factory-method attribute denotes the name of actual method of the class.

5. Purpose of Static Factory Method?
a) Static method to create an object
b) Initialize bean
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: a
Explanation: Instantiate a bean using static method.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. Exception thrown by factory method?
a) IllegalArgumentException
b) IndexOutofBoundException
c) ClassPathNotFoundException
d) BeanCreationException
View Answer

Answer: d
Explanation: Spring generates the above mentioned exception, in case something’s wrong.

7. What will be the output?
Snippet of Code:

advertisement
public class CreatePro {
    String ProductId;
    public CreatePro(String ProductId)	{
        this.ProductId = ProductId;
    }
 
public static Product creation_Product(String productId) {	
    System.out.println("Bean Created");
    if ("aaa".equals(productId)) {
        return new Battery("AAA", 2.5);
    } else if ("cdrw".equals(productId)) {
        return new Disc("CD-RW", 1.5);
    }
}
}
<beans ...>
<bean id="aaa" class="CreatePro"
    factory-method="createProduct">
    <constructor-arg value="aaa" />
</bean>
<bean id="cdrw" class="CreatePro"
    factory-method="createProduct">
    <constructor-arg value="cdrw" />
</bean>
</beans>

a) BeanCreationException
b) Bean Created
c) ClassPathException
d) None of the mentioned
View Answer

Answer: a
Explanation: Since factory-method doesn’t exists in the ProductCreator class, so exception thrown.
Output: BeanCreationException
advertisement

8. A bean can have more than one name using multiple id attributes?
a) True
b) False
View Answer

Answer: a
Explanation: Beans are allowed to have more than one ids.

9. Bean’s naming convention:-
starts with lowercase, camelcase from then on.?
a) True
b) False
View Answer

Answer: a
Explanation: Beans follow naming conventions.

10. Beans can be created by which of the following properties?
a) Static factory-method
b) Instance Factory-Method
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: Instantiate a bean via static and instance Factory methods.

11. The bean instance is mentioned by the factory-method attribute, while the factory method is signified by the factory-bean attribute?
a) True
b) False
View Answer

Answer: b
Explanation: The bean instance is mentioned by factory-bean attr while factory method is for factory-method attr.

12. One factory class can also hold more than one factory method True/False?
a) True
b) False
View Answer

Answer: a
Explanation: A single instantiated bean can have more than one methods.

13. Snippet of Code:

public class CreatePro 
{
     String ProductId;
     public CreatePro(String ProductId)	
     this.ProductId = ProductId;
}
 
public static Product creation_Product(String productId) 
{	
     System.out.println("Bean Created");
     if ("aaa".equals(productId)) 
     {
        return new Battery("AAA", 2.5);
     } 
     else if ("cdrw".equals(productId)) 
     {
	return new Disc("CD-RW", 1.5);
     }
 
}
 
  <beans ...>
	<bean id="aaa" class="CreatePro"
	factory-method="createProduct">
	<constructor-arg value="aaa" />
	</bean>
	<bean id="cdrw" class="CreatePro"
	factory-method="createProduct">
	<constructor-arg value="cdrw" />
	</bean>
  </beans>
  slight change in XML file:-
 
<bean id="aaa" factory-bean="productCreator"
factory-method="createProduct">
<constructor-arg value="aaa" />
</bean>
<bean id="cdrw" factory-bean="productCreator"
factory-method="createProduct">
<constructor-arg value="cdrw" />
</bean>

What will be the output:-
a) BeanCreationException
b) IllegalArgumentException
c) New Product will be created
d) None of the mentioned
View Answer

Answer: c
Explanation: Factory-Bean is used instead of class.

14. Instance Factory method main purpose is to encapsulate the object-creation process in a method of another object instance.
a) True
b) False
View Answer

Answer: a
Explanation: The client who requests an object can simply make a call to this method without knowing about the creation detail.

15. Which Attribute is used to specify the bean declared?
a) factory-bean
b) scope
c) getBean
d) declareBean
View Answer

Answer: a
Explanation: To declare a bean created by an instance factory method, you specify the bean hosting the factory method in the factory-bean attribute.

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.