adplus-dvertising
frame-decoration

Question

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

Posted under Java Spring Framework

Answer: (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)

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What will be the output?

Similar Questions

Discover Related MCQs

Q. Which interface is used to perform initialization of beans?

Q. Which interface is used to perform destruction of beans?

Q. Alternate way of initialization method is:-

Q. Alternate way of destruction method is:-

Q. Which annotation is used as a substitute of initialization method?

Q. Which annotation is used as a substitute of destroy method?

Q. Which configuration can be used for Dependency Injection?

Q. Method used to process bean before initialization callback

Q. Method used to process bean after initialization callback

Q. Which method is used to gracefully shutdown all the bean processes after closing the spring container?

Q. Which method is used to register BeanPostProcessor?

Q. In application context, BeanPost Processors are registered using addBeanPostProcessors method

Q. Which Interface for bean Post Processor is used to distinguish between checked beans

Q. Which method of bean post processors is used to check path existence

Q. It’s possible to replace the original bean instance with a brand-new instance in your bean post processor

Q. PathCheckingBeanPostProcessor will not work properly in case of:-

Q. Which bean factory post processor externalizes part of the bean configurations into a properties file

Q. Which interface defines methods for resolving text messages

Q. Which interface is used to listen to certain events

Q. Which method is used to publish your own custom event