This set of Java Multiple Choice Questions & Answers (MCQs) on “Restrictions on Generics”.
1. Which of these types cannot be used to initiate a generic type? Answer: c 2. Which of these instance cannot be created? Answer: c 3. Which of these data type cannot be type parameterized? Answer: a 4. What will be the output of the following Java program? a) 10 5. What will be the output of the following Java program? a) Error 6. What will be the output of the following Java program? a) 5.0 7. What will be the output of the following Java program? a) 1 8. What will be the output of the following Java program? a) H
a) Integer class
b) Float class
c) Primitive Types
d) Collections
Clarification: None.
a) Integer instance
b) Generic class instance
c) Generic type instance
d) Collection instances
Clarification: It is not possible to create generic type instances. Example – “E obj = new E()” will give a compilation error.
a) Array
b) List
c) Map
d) Set
Clarification: None.
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("Box #" + counter + " contains [" + boxContents.toString() + "]");
counter++;
}
}
public static void main(String[] args)
{
java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
BoxDemo.outputBoxes(listOfIntegerBoxes);
}
}
b) Box #0 [10]
c) Box contains [10]
d) Box #0 contains [10]
Clarification: None.
Output:
$ javac Output.java
$ java Output
Box #0 contains [10]
import java.util.*;
public class genericstack <E>
{
Stack <E> stk = new Stack <E>();
public void push(E obj)
{
stk.push(obj);
}
public E pop()
{
E obj = stk.pop();
return obj;
}
}
class Output
{
public static void main(String args[])
{
genericstack <String> gs = new genericstack<String>();
gs.push("Hello");
System.out.print(gs.pop() + " ");
genericstack <Integer> gs = new genericstack<Integer>();
gs.push(36);
System.out.println(gs.pop());
}
}
b) Hello
c) 36
d) Hello 36
Clarification: None.
Output:
$ javac Output.java
$ java Output
Hello 36
import java.util.*;
class Output
{
public static double sumOfList(List extends Number> list)
{
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}
public static void main(String args[])
{
List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);
System.out.println(sumOfList(ld));
}
}
b) 7.0
c) 8.0
d) 6.0
Clarification: None.
Output:
$ javac Output.java
$ java Output
7.0
import java.util.*;
class Output
{
public static void addNumbers(List super Integer> list)
{
for (int i = 1; i <= 10; i++)
{
list.add(i);
}
}
public static void main(String args[])
{
List<Double> ld = Arrays.asList();
addnumbers(10.4);
System.out.println("getList(2)");
}
}
b) 2
c) 3
d) 6
Clarification: None.
Output:
$ javac Output.java
$ java Output
1
import java.util.*;
public class genericstack <E>
{
Stack <E> stk = new Stack <E>();
public void push(E obj)
{
stk.push(obj);
}
public E pop()
{
E obj = stk.pop();
return obj;
}
}
class Output
{
public static void main(String args[])
{
genericstack <Integer> gs = new genericstack<Integer>();
gs.push(36);
System.out.println(gs.pop());
}
}
b) Hello
c) Runtime Error
d) Compilation Error
Clarification: generic stack object gs is defined to contain a string parameter but we are sending an integer parameter, which results in compilation error.
Output:
