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

Answer:

b) A feature that allows types to be parameters in methods, classes, and interfaces

Explanation:

Generics in Java are a language feature that allows developers to write methods, classes, and interfaces with a placeholder for types, which can be specified when the code is used.

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

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

Answer:

a) <>

Explanation:

In Java, angle brackets <> are used to define a generic type.

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> {}

Answer:

a) class MyClass<T> {}

Explanation:

A generic class in Java is defined with the class name followed by a type parameter in angle brackets.

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

Answer:

b) Converting generic types to Object during runtime

Explanation:

Type erasure is a process where the compiler replaces generic parameters with their bounds or Object if the type parameters are unbounded.

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

Answer:

b) For improved type safety

Explanation:

Generics are used to provide tighter type checks at compile time and to support generic programming.

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

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

Answer:

a) <T extends Number>

Explanation:

Bounded type parameters are declared using the extends keyword, followed by the upper bound, like <T extends 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

Answer:

a) Yes

Explanation:

A generic class can have multiple type parameters separated by commas, like <K, V>.

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

Answer:

c) An unknown type, represented by '?'

Explanation:

Wildcards in Java generics are represented by '?', allowing a variable to hold a value of an unknown 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() {}

Answer:

a) <T> returnType methodName(T param) {}

Explanation:

A generic method is defined with the type parameter before the return type.

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

Answer:

a) Any type that extends T or T itself

Explanation:

The <? extends T> wildcard represents an upper-bounded wildcard, which can be any type that extends T, including T itself.

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

Answer:

a) Any type that is a superclass of T

Explanation:

The <? super T> wildcard represents a lower-bounded wildcard, meaning any type that is a superclass of T.

12. Can generics be used with primitive types?

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

Answer:

b) No

Explanation:

Generics in Java cannot be used with primitive types directly; they require wrapper classes.

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

Answer:

a) To simplify instantiation of generics

Explanation:

The diamond operator allows the compiler to infer the type parameters, simplifying instantiation of generic types.

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> {}

Answer:

a) interface MyInterface<T> {}

Explanation:

A generic interface is created similar to a generic class, with the type parameter in angle brackets after the interface name.

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

Answer:

c) The code compiles and runs, but loses type safety

Explanation:

Using raw types with generics is legal but not recommended, as it bypasses generic type checks and can lead to runtime errors.

16. Can a generic type parameter have multiple bounds?

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

Answer:

a) Yes

Explanation:

A generic type parameter can have multiple bounds using the & symbol, like <T extends Number & Comparable<T>>.

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

Answer:

c) Type safety and reduced need for type casting

Explanation:

Generics in collections provide type safety and eliminate the need for explicit type casting when retrieving 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

Answer:

a) By specifying the generic type in the implementing class

Explanation:

When implementing a generic interface, the type parameter must be specified in the implementing class or another generic 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

Answer:

a) Yes

Explanation:

A generic class can extend a non-generic class, and a non-generic class can also extend a generic class with specific type arguments.

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

Answer:

b) Inferring the type parameters of a generic method or class

Explanation:

Type inference is the capability of the compiler to determine the type parameters of a generic method or class based on the context, such as the types of the arguments or the expected return type.


Comments