This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Restrictions on Generics”.
1. Which of these types cannot be used to initiate a generic type?
a) Integer class
b) Float class
c) Primitive Types
d) Collections
View Answer
Explanation: None.
2. Which of these instance cannot be created?
a) Integer instance
b) Generic class instance
c) Generic type instance
d) Collection instances
View Answer
Explanation: It is not possible to create generic type instances. Example – “E obj = new E()” will give a compilation error.
3. Which of these data type cannot be type parameterized?
a) Array
b) List
c) Map
d) Set
View Answer
Explanation: None.
4. 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("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);
}
}
a) 10
b) Box #0 [10]
c) Box contains [10]
d) Box #0 contains [10]
View Answer
Explanation: None.
Output:
$ javac Output.java $ java Output Box #0 contains [10]
5. What will be the output of the following Java program?
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());
}
}
a) Error
b) Hello
c) 36
d) Hello 36
View Answer
Explanation: None.
Output:
$ javac Output.java $ java Output Hello 36
6. What will be the output of the following Java program?
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));
}
}
a) 5.0
b) 7.0
c) 8.0
d) 6.0
View Answer
Explanation: None.
Output:
$ javac Output.java $ java Output 7.0
7. What will be the output of the following Java program?
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)");
}
}
a) 1
b) 2
c) 3
d) 6
View Answer
Explanation: None.
Output:
$ javac Output.java $ java Output 1
8. What will be the output of the following Java program?
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());
}
}
a) H
b) Hello
c) Runtime Error
d) Compilation Error
View Answer
Explanation: generic stack object gs is defined to contain a string parameter but we are sending an integer parameter, which results in compilation error.
Output:
$ javac Output.java
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Java Internship
- Practice Programming MCQs
- Practice BCA MCQs
- Apply for Computer Science Internship
- Check Java Books