Search Tutorials


Java Generics MCQ Questions and Answers | JavaInUse

Java Generics MCQ Questions and Answers

Q. What is the syntax for declaring a generic class in Java?

A. class MyClass{}
B. 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 time
B. 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 type
B. 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 T method(T t)
C. public method(T t)
D. public T(T t)

Q. What is the difference between List and List?

A. They are the same thing
B. 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 types
B. 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 successfully
B. 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 class
B. 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 parameters
B. 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 performance
B. 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 completely
B. It's erased due to type erasure
C. It's partially preserved
D. It depends on JVM settings





Q. Which of these is a valid generic interface declaration?

A. interface MyInterface[T]
B. interface MyInterface(T)
C. interface MyInterface
D. generic interface MyInterface

Q. What is the PECS principle in Java generics?

A. Producer Extends, Consumer Super
B. Producer Extends, Compiler Super
C. Private Extends, Common Super
D. Public Extends, Class Super

Q. What is a raw type in Java generics?

A. A generic type without type parameters
B. A primitive type
C. An uncompiled type
D. A type that hasn't been processed

Q. Which of these is true about generic methods?

A. They can only be used in generic classes
B. They must have generic parameters
C. They can be static or non-static
D. They cannot be overloaded

Q. What is the result of using an unbounded wildcard (?) in a generic type?

A. It can only accept Object type
B. It can accept any type
C. It can only accept primitive types
D. It causes a compilation error

Q. What happens when you try to create an array of generic type like T[]?

A. It works fine
B. Runtime exception
C. Compilation warning
D. Compilation error in most cases

Q. Which of these is a valid multiple bounds declaration?

A.
B.
C.
D.

Q. What happens when you try to catch an exception of a generic type?

A. Works perfectly fine
B. Compilation error
C. Runtime exception
D. Warning message

Q. What is the syntax for declaring a generic constructor?

A. public MyClass(T t)
B. public MyClass(T t)
C. public T MyClass(T t)
D. public MyClass(T t)

Q. What is the bridge method in Java generics?

A. A method to connect generic and non-generic code
B. A synthetic method created by compiler for type erasure
C. A method to handle generic exceptions
D. A method to convert primitive types to generic types

Q. Which of these is true about generic type inference in Java 8 and later?

A. It only works with lambda expressions
B. It requires explicit type declarations
C. It supports target typing
D. It doesn't work with method references

Q. What is the difference between List<?> and List<Object>?

A. They are exactly the same
B. List is more flexible for reading
C. List is more flexible
D. List cannot be used for reading

Q. What happens when a generic class extends a non-generic class?

A. Compilation error
B. It's perfectly valid
C. Runtime error
D. Warning message

Q. What is the purpose of the @SuppressWarnings("unchecked") annotation in generics?

A. To prevent type erasure
B. To suppress compiler warnings about unsafe operations
C. To enable raw type usage
D. To allow primitive types in generics

Q. Which of these correctly describes generic type parameter naming conventions?

A. Must be single uppercase letters
B. Can be any valid identifier
C. Should be single uppercase letters by convention
D. Must be T, E, or K

Q. What happens when you try to overload a method with different generic type parameters?

A. Compilation error due to type erasure
B. Works perfectly fine
C. Runtime error
D. Warning message

Q. What is the reifiable type in Java generics?

A. A type whose runtime representation contains less information than its compile-time counterpart
B. A type whose runtime representation contains all information available at compile time
C. A type that can only be used with primitives
D. A type that cannot be used with generics

Q. What is the primary use of recursive type bounds?

A. To create infinite type hierarchies
B. To compare types that implement Comparable
C. To create recursive data structures
D. To improve performance

Q. Which statement about generic varargs is correct?

A. They are completely type-safe
B. They generate heap pollution warnings
C. They are not allowed in Java
D. They only work with primitive types