Java Generics MCQ Questions and Answers

1. What are generics in Java?

a) A method of creating generic algorithms
b) A feature that allows types to be parameters in methods, classes, and interfaces
c) A tool for dynamic type casting
d) A way to create generalized APIs

2. Which symbol is used to define a generic type in Java?

a) <>
b) []
c) {}
d) ()

3. How do you define a generic class in Java?

a) class MyClass<T> {}
b) class MyClass(Generic T) {}
c) class <T>MyClass {}
d) class MyClass implements Generic<T> {}

4. What is type erasure in the context of Java generics?

a) Removing types from a class
b) Converting generic types to Object during runtime
c) Erasing the value of a variable
d) Discarding type information in compiled code

5. Why are generics used in Java?

a) For faster performance
b) For improved type safety
c) To avoid using primitive types
d) To increase application size

6. How do you declare a bounded type parameter in generics?

a) <T extends Number>
b) <T Number>
c) <Number T>
d) <T = Number>

7. Can a generic class have multiple type parameters?

a) Yes
b) No
c) Only in static methods
d) Only if they are of the same type

8. What is a wildcard in Java generics?

a) A generic type that can hold any data type
b) A special character used in generic methods
c) An unknown type, represented by '?'
d) A placeholder for one specific type

9. How do you define a generic method in Java?

a) <T> returnType methodName(T param) {}
b) returnType <T> methodName(T param) {}
c) returnType methodName<T>(T param) {}
d) <T> returnType methodName() {}

10. What does the <? extends T> wildcard represent?

a) Any type that extends T or T itself
b) Any type that is a superclass of T
c) A specific implementation of T
d) Any type, without restrictions

11. What does the <? super T> wildcard represent?

a) Any type that is a superclass of T
b) Any type that extends T or T itself
c) A specific implementation of T
d) Any type, without restrictions

12. Can generics be used with primitive types?

a) Yes
b) No
c) Only with certain primitive types
d) Only in specific scenarios

13. What is the purpose of the diamond operator (<>), introduced in Java 7?

a) To simplify instantiation of generics
b) To explicitly set the type of a generic
c) To denote an empty generic type
d) To create anonymous classes

14. How do you create a generic interface in Java?

a) interface MyInterface<T> {}
b) <T> interface MyInterface {}
c) interface <T>MyInterface {}
d) interface MyInterface implements Generic<T> {}

15. What happens if you assign a raw type to a generic type?

a) Compile-time error
b) Runtime error
c) The code compiles and runs, but loses type safety
d) Automatic type conversion occurs

16. Can a generic type parameter have multiple bounds?

a) Yes
b) No
c) Only in interfaces
d) Only in abstract classes

17. What is the main benefit of using generics in a collection?

a) Increased performance
b) Reduced memory usage
c) Type safety and reduced need for type casting
d) Automatic sorting of elements

18. How do you implement a generic interface?

a) By specifying the generic type in the implementing class
b) By creating an anonymous class
c) By using a wildcard
d) By omitting the type parameter

19. Can a generic class extend a non-generic class?

a) Yes
b) No
c) Only if the non-generic class is abstract
d) Only if the non-generic class implements an interface

20. What is type inference in the context of Java generics?

a) Automatically determining the type of a variable
b) Inferring the type parameters of a generic method or class
c) Casting objects to generic types
d) Determining the type of a collection

Comments