Java MCQ – Generic types
This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Generic types”.
1. What is the purpose of Generic types in Java?
A It makes the code faster
B It makes the code more optimized and more readable
C It adds stability to your code by making your bugs detectable at runtime
D It adds stability to your code by making your bugs detectable at compile time
2. Which of these type of parameters is used for a generic class to return and accept any type of object?
A V
B T
C N
D K
3. Which of these type parameters is used for a generic class to return and accept a number?
A V
B T
C N
D K
4. Which of the following types of reference cannot be generic?
A Inner classes
B Interface
C Inner class anonymous
D All the answers are true
5. Which of the following statements is incorrect about the use of generic and parameterized types in Java?
A When designing your own class of collections, generic and parameterized types allow you to achieve type security with a single class definition rather than defining multiple classes
B Generic and parameterized types removes the need for top-down conversions when using Java collections
C Generic type in Java ensures type safety by transferring type checking responsibilities to the compiler
D All the answers are true
6. Which of the following methods is correct for defining a generic class?
A class MyClass2 <T1, T2, ....., Tn> { /* … */ }
B class MyClass4 {T1, T2, …, Tn} { /* … */ }
C class MyClass3 [T1, T2, …, Tn] { /* … */ }
D class MyClass1(T1, T2, …, Tn) { /* … */ }
7. What is the output of this program?
import java.util.*; class GenericStack <E> { Stack <E> stack = new Stack <E>(); public E pop() { E obj = stack.pop(); return obj; } public void push(E obj) { stack.push(obj); } } public class Test { public static void main(String args[]) { GenericStack <String> gs = new GenericStack <String>(); gs.push("Hello"); System.out.println(gs.pop()); } }
A Hello
B Compilation error
C obj478962
D 44
8. What is the output of this program?
import java.util.*; class GenericStack <E> { Stack <E> stack = new Stack <E>(); public E pop() { E obj = stack.pop(); return obj; } public void push(E obj) { stack.push(obj); } } public class Test { public static void main(String args[]) { GenericStack <Integer> gs = new GenericStack <Integer>(); gs.push(44); System.out.println(gs.pop()); } }
A Compilation error
B Runtime error
C obj478962
D 44
9. What is the output of this program?
import java.util.*; class GenericStack <E> { Stack <E> stack = new Stack <E>(); public void push(E obj) { stack.push(obj); } public E pop() { E obj = stack.pop(); return obj; } } public class Test { 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(44); System.out.println(gs.pop()); } }
A Hello 44
B Compilation error
C Hello
D 44