Java Questions & Answers – Restrictions on Generics

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

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

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

Answer: a
Explanation: None.
advertisement
advertisement

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]
View Answer

Answer: d
Explanation: None.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ javac Output.java
$ java Output
Box #0 contains [10]

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

advertisement
  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
View Answer

Answer: d
Explanation: None.
Output:

advertisement
$ 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: d
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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.