250+ TOP MCQs on Restrictions on Generics and Answers

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?
a) Integer class
b) Float class
c) Primitive Types
d) Collections

Answer: c
Clarification: None.

2. Which of these instance cannot be created?
a) Integer instance
b) Generic class instance
c) Generic type instance
d) Collection instances

Answer: c
Clarification: 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

Answer: a
Clarification: None.

4. What will be the output of the following Java program?

  1.     public class BoxDemo
  2.     {
  3.         public static <U> void addBox(U u, 
  4.            java.util.List<Box<U>> boxes)
  5.           {
  6.             Box<U> box = new Box<>();
  7.             box.set(u);
  8.             boxes.add(box);
  9.           }
  10.         public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
  11.         {
  12.             int counter = 0;
  13.             for (Box<U> box: boxes)
  14.             {
  15.                 U boxContents = box.get();
  16.                 System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
  17.                 counter++;
  18.             }
  19.         }
  20.         public static void main(String[] args)
  21.         {
  22.             java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
  23.             BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
  24.             BoxDemo.outputBoxes(listOfIntegerBoxes);
  25.         }
  26.     }

a) 10
b) Box #0 [10]
c) Box contains [10]
d) Box #0 contains [10]

Answer: d
Clarification: None.
Output:

$ javac Output.java
$ java Output
Box #0 contains [10]

5. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. 	public void push(E obj) 
  6.         {
  7.             stk.push(obj);
  8. 	}
  9. 	public E pop() 
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. 	}
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <String> gs = new genericstack<String>();
  20.             gs.push("Hello");
  21.             System.out.print(gs.pop() + " ");
  22.             genericstack <Integer> gs = new genericstack<Integer>();
  23.             gs.push(36);
  24.             System.out.println(gs.pop());
  25.         }
  26.     }

a) Error
b) Hello
c) 36
d) Hello 36

Answer: d
Clarification: None.
Output:

$ javac Output.java
$ java Output
Hello 36

6. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Output
  3.     {
  4.         public static double sumOfList(List extends Number> list) 
  5.         {
  6.             double s = 0.0;
  7.             for (Number n : list)
  8.                 s += n.doubleValue();
  9.             return s;
  10.         }
  11.         public static void main(String args[])
  12.         {
  13.            List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);
  14.            System.out.println(sumOfList(ld));
  15.         }
  16.     }

a) 5.0
b) 7.0
c) 8.0
d) 6.0

Answer: b
Clarification: None.
Output:

$ javac Output.java
$ java Output
7.0

7. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Output
  3.     {
  4.         public static void addNumbers(List super Integer> list)
  5.         {
  6.             for (int i = 1; i <= 10; i++)
  7.             {
  8.                 list.add(i);
  9.             }
  10.         }
  11.         public static void main(String args[])
  12.         {
  13.            List<Double> ld = Arrays.asList();
  14.            addnumbers(10.4);
  15.            System.out.println("getList(2)");
  16.         }
  17.     }

a) 1
b) 2
c) 3
d) 6

Answer: a
Clarification: None.
Output:

$ javac Output.java
$ java Output
1

8. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. 	public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. 	}
  9. 	public E pop()
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. 	}
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[]) 
  18.         {
  19.             genericstack <Integer> gs = new genericstack<Integer>();
  20.             gs.push(36);
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) H
b) Hello
c) Runtime Error
d) Compilation Error

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top