Examveda
Examveda

What will be the output of the following Java program?
public class BoxDemo
{
    public static <U> void addBox(U u, 
    java.util.List<Box<U>> boxes)
    {
       Box<U> box = new Box<>();
       box.set(u);
       boxes.add(box);
    }
    public static <U> void outputBoxes(java.util.List<Box<U>> boxes) 
    {
        int counter = 0;
        for (Box<U> box: boxes) 
        {
            U boxContents = box.get();
            System.out.println("[" + boxContents.toString() + "]");
            counter++;
        }
    }
    public static void main(String[] args)
    {
        java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
        BoxDemo.<Integer>addBox(Integer.valueOf(0), listOfIntegerBoxes);
        BoxDemo.outputBoxes(listOfIntegerBoxes);
    }
}

A. 0

B. 1

C. [1]

D. [0]

Answer: Option D


This Question Belongs to Java Program >> Interfaces And Abstract Classes

Join The Discussion

Related Questions on Interfaces and Abstract Classes

What is an interface in Java?

A. A contract specifying a set of methods that a class must implement

B. A class that cannot be instantiated

C. A class that contains only static methods

D. A subclass of the Object class