Java Generics MCQ Questions and Answers
Q. What is the syntax for declaring a generic class in Java?
A. class MyClassB. class MyClass[T]{}
C. class MyClass(T){}
D. generic class MyClass
Q. What is type erasure in Java generics?
A. Removing all generics at compile timeB. Converting generics to their raw types at runtime
C. Process of removing generic type information during compilation
D. Deleting unused generic classes
Q. What is the purpose of wildcard ? in Java generics?
A. To represent any typeB. To represent only numeric types
C. To represent unknown types
D. To represent null types
Q. Which of these is a valid generic method declaration?
A. public T method(T t)B. public
C. public method
D. public T
Q. What is the difference between List extends Number> and List super Integer>?
A. They are the same thingB. First is upper bounded, second is lower bounded
C. First is lower bounded, second is upper bounded
D. Both are upper bounded
Q. Which of these is NOT possible with Java generics?
A. Creating arrays of generic typesB. Using primitive types directly as type arguments
C. Having static fields of generic type
D. All of the above
Q. What is the output of List<Object> list = new ArrayList();?
A. Compiles successfullyB. Runtime error
C. Compilation error
D. Depends on JVM version
Q. What is the purpose of bounded type parameters?
A. To restrict the types that can be used with a generic classB. To improve performance
C. To create multiple instances of generic class
D. To avoid type erasure
Q. Which statement about generic type inference is correct?
A. It only works with method parametersB. It was introduced in Java 5
C. It allows the compiler to determine generic types
D. It only works with collections
Q. What is the primary benefit of using generics?
A. Improved performanceB. Type safety at compile time
C. Reduced memory usage
D. Better runtime performance
Q. What happens to generic type information at runtime?
A. It's preserved completelyB. It's erased due to type erasure
C. It's partially preserved
D. It depends on JVM settings