Java Spring Multiple Choice Questions & Answers (MCQs) on “Bean Scopes”.
1. A bean can be requested by:-
a) getBean method
b) reference from another bean using autowiring, property etc
c) all of the mentioned
d) none of the mentioned
Answer: c
Clarification: When a bean is requested by the getBean() method or a reference from other beans, Spring will decide which bean instance should be returned according to the bean scope.
2. Which attribute is used to set the scope of the bean?
a) setScope
b) scope
c) getScope
d) none of the mentioned
Answer: b
Clarification: Scope attribute defines the scope of a bean.
3. Which one is the default scope of the beans?
a) Prototype
b) Session
c) Request
d) Singleton
Answer: d
Clarification: This unique bean instance will be returned for all subsequent getBean() calls and bean references.
4. Which scope creates a new bean instance each time when requested?
a) Singleton
b) Prototype
c) Session
d) Request
Answer: b
Clarification: Creates a new bean instance each time when requested.
5. Session Creates a single bean instance per HTTP request, only valid in the context of a web application?
a) True
b) False
Answer: b
Clarification: Session Creates a single bean instance per HTTP session, only valid in the context of a web application.
6. Which of the following are considered valid beans?
a) Singleton
b) Prototype
c) All of the mentioned
d) None of the mentioned
Answer: c
Clarification: In Spring 1.x, singleton and prototype are the only two valid bean scopes, and they are specified by the singleton attribute (i.e., singleton=”true” or singleton=”false”), not the scope attribute.
7. What will be the output?
public class ShoppingCart { private List<Product> items = new ArrayList<Product>(); public void addItem(Product item) { items.add(item); } public List<Product> getItems() { return items; } } <beans ...> <bean id="aaa" class="com.shop.Battery"> <property name="name" value="AAA" /> <property name="price" value="2.5" /> bean> <bean id="cdrw" class="com.shop.Disc"> <property name="name" value="CD-RW" /> <property name="price" value="1.5" /> bean> <bean id="dvdrw" class="com.shop.Disc"> <property name="name" value="DVD-RW" /> <property name="price" value="3.0" /> bean> <bean id="shoppingCart" class="com.shop.ShoppingCart" /> beans> import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Product aaa = (Product) context.getBean("aaa"); Product cdrw = (Product) context.getBean("cdrw"); Product dvdrw = (Product) context.getBean("dvdrw"); ShoppingCart cart1 = (ShoppingCart) context.getBean("shoppingCart"); cart1.addItem(aaa); cart1.addItem(cdrw); System.out.println("Shopping cart 1 contains " + cart1.getItems()); ShoppingCart cart2 = (ShoppingCart) context.getBean("shoppingCart"); cart2.addItem(dvdrw); System.out.println("Shopping cart 2 contains " + cart2.getItems()); } }
a) Shopping cart 1 contains (AAA 2.5, CD-RW 1.5)
Shopping cart 2 contains (AAA 2.5, CD-RW 1.5, DVD-RW 3.0)
b) Shopping cart 1 contains (AAA 2.5, CD-RW 1.5)
Shopping cart 2 contains (DVD-RW 3.0)
c) BeanCreationException
d) None of the mentioned
Answer: a
Clarification: Since the default scope is singleton, so items added by first bean instantiated will be used again by same bean if instantiated again.
8. In above question if scope of shoppingCart named bean is prototype, then what will be the output?
What will be the output?
a) Shopping cart 1 contains (AAA 2.5, CD-RW 1.5)
Shopping cart 2 contains (AAA 2.5, CD-RW 1.5, DVD-RW 3.0)
b) Shopping cart 1 contains (AAA 2.5, CD-RW 1.5)
Shopping cart 2 contains (DVD-RW 3.0)
c) BeanCreationException
d) None of the mentioned
Answer: b
Clarification: Since the default scope is prototype, so items added by first bean instantiated will be removed by same bean if instantiated again, and will add new items listed.
9. Which interface is used to perform initialization of beans?
a) InitializingBean
b) Disposablebean
c) None of the mentioned
d) All of the mentioned
Answer: a
Clarification: Spring allows your bean to perform initialization callback methods afterPropertiesSet() by implementing the InitializingBean and interfaces.
10. Which interface is used to perform destruction of beans?
a) InitializingBean
b) Disposablebean
c) None of the mentioned
d) All of the mentioned
Answer: b
Clarification: Spring allows your bean to perform destroy callback methods destroy() by implementing the DisposableBean and interfaces.
11. Alternate way of initialization method is:-
a) init-method attribute
b) afterPropertiesSet
c) destroy-method attribute
d) none of the mentioned
Answer: a
Clarification: A better approach of specifying the initialization callback methods is by setting the init-method attributes in your bean declaration.
12. Alternate way of destruction method is:-
a) init-method attribute
b) afterPropertiesSet
c) destroy-method attribute
d) none of the mentioned
Answer: c
Clarification: A better approach of specifying the destroy callback methods is by setting the destroy-method attributes in your bean declaration.
13. Which annotation is used as a substitute of initialization method?
a) @PostConstruct
b) @PreDestroy
c) None of the mentioned
d) All of the mentioned
Answer: a
Clarification: Using JSR annotation.
14.Which annotation is used as a substitute of destroy method?
a) @PostConstruct
b) @PreDestroy
c) None of the mentioned
d) All of the mentioned
Answer: b
Clarification: Using JSR annotation.
15. Which configuration can be used for Dependency Injection?
a) XML Configuration
b) Annotation Configuration
c) Java Based Configuration
d) All of the mentioned
Answer: d
Clarification: All of the above mentioned can be used for Dependency Injection.