Java Spring Multiple Choice Questions & Answers (MCQs) 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
Answer: d
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: Class attribute is used to specify the name of the class that contains the static factory method.
4. Which attribute is used to specify static factory-method?
a) factory-method
b) default-init method
c) destroy method
d) lazy-init method
Answer: a
Clarification: 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
Answer: a
Clarification: Instantiate a bean using static method.
6. Exception thrown by factory method?
a) IllegalArgumentException
b) IndexOutofBoundException
c) ClassPathNotFoundException
d) BeanCreationException
Answer: d
Clarification: Spring generates the above mentioned exception, in case something’s wrong.
7. What will be the output?
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>
a) BeanCreationException
b) Bean Created
c) ClassPathException
d) None of the mentioned
Answer: a
Clarification: Since factory-method doesn’t exists in the ProductCreator class, so exception thrown.
Output: BeanCreationException
8. A bean can have more than one name using multiple id attributes?
a) True
b) False
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: To declare a bean created by an instance factory method, you specify the bean hosting the factory method in the factory-bean attribute.