Java Programming — Generics
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 100 questions in Generics
Q.1 Medium Generics
In a microservices architecture, you're designing a Response wrapper class that should work with any data type, including primitives when boxed. Which implementation is correct?
A public class Response { private T data; private int statusCode; }
B public class Response { private Object data; private int statusCode; }
C public class Response { private T data; private int statusCode; }
D public class Response { private T data; private S statusCode; }
Correct Answer:  A. public class Response { private T data; private int statusCode; }
EXPLANATION

Option A provides proper generic type safety without unnecessary constraints. While Option C adds a Serializable bound (sometimes desirable), Option A is the most flexible and commonly used pattern for generic response wrappers.

Take Test
Q.2 Hard Generics
A method processes collections where items need to be added back to the same collection type. Which wildcard approach ensures type safety for write operations?
A public static void addElements(List
B public static void addElements(List destination, List source)
C public static void addElements(List destination, List source)
D public static void addElements(List destination, List source)
Correct Answer:  A. public static void addElements(List
EXPLANATION

'? super T' allows writing T instances (contravariant), while '? extends T' allows safe reading (covariant). This combination enables flexible yet type-safe collection manipulation.

Take Test
Q.3 Easy Generics
When using bounded type parameters with multiple interfaces, what is the correct syntax in Java?
A
B
C
D
Correct Answer:  A.
EXPLANATION

Java uses ampersand (&) to combine multiple bounds. The class must appear first, followed by interfaces. Commas, pipes, and 'implements' keyword are not valid syntax for type parameter bounds.

Take Test
Q.4 Medium Generics
A utility class needs a method that accepts a List containing elements that are instances of a specific class or its subclasses. Which wildcard notation should be used?
A public static void process(List
B public static void process(List list)
C public static void process(List
D public static void process(List list)
Correct Answer:  A. public static void process(List
EXPLANATION

'? extends TargetClass' creates a covariant wildcard allowing lists of TargetClass or any subclass. This is ideal for reading operations when you need polymorphic list handling.

Take Test
Q.5 Easy Generics
In a Spring Boot application, you need to create a generic repository interface that works with any entity type. Which declaration correctly implements this pattern?
A public interface GenericRepository { T findById(Long id); void save(T entity); }
B public interface GenericRepository { Object findById(Long id); void save(Object entity); }
C public interface GenericRepository { T findById(Long id); void save(T entity); }
D public interface GenericRepository { T findById(K id); void save(T entity); }
Correct Answer:  A. public interface GenericRepository { T findById(Long id); void save(T entity); }
EXPLANATION

Option A uses single type parameter T for entity type, which is the standard pattern for generic repositories. Option C is redundant (all classes extend Object), and Option D adds unnecessary complexity.

Take Test
Advertisement
Q.6 Medium Generics
A developer creates a generic method that accepts a collection of numbers and calculates their sum. Which type parameter declaration would be most appropriate for this scenario?
A public static double calculateSum(List numbers)
B public static double calculateSum(List numbers)
C public static double calculateSum(List numbers)
D public static double calculateSum(List numbers)
Correct Answer:  A. public static double calculateSum(List numbers)
EXPLANATION

Using 'T extends Number' establishes an upper bound, allowing the method to work with any Number subclass (Integer, Double, Float, etc.) while providing type safety and access to Number methods.

Take Test
Q.7 Medium Generics
What is the runtime class object for this generic variable?
java
List list = new ArrayList();
Class c = list.getClass();
A java.util.List
B java.util.ArrayList
C java.util.ArrayList
D java.lang.Object
Correct Answer:  C. java.util.ArrayList
EXPLANATION

Due to type erasure, getClass() returns ArrayList without generic parameters. Generic information exists only at compile time.

Take Test
Q.8 Hard Generics
Which Java feature was introduced specifically to support generics compilation?
A Bridge methods
B Reflection API
C Annotations
D Lambda expressions
Correct Answer:  A. Bridge methods
EXPLANATION

Bridge methods are synthetic methods generated during compilation to maintain polymorphism with generic types and type erasure.

Take Test
Q.9 Hard Generics
What output will this produce?
java
List
A Compiles and runs successfully, prints 5
B Compilation error
C Runtime ClassCastException
D Returns null
Correct Answer:  C. Runtime ClassCastException
EXPLANATION

With lower-bounded wildcard (? super Integer), you can add Integer but retrieval returns Object. Casting Object to Integer without proper type causes ClassCastException.

Take Test
Q.10 Hard Generics
Which statement about generic array creation is correct?
A new ArrayList[10] is valid
B new List[10] is valid but produces warning
C Generic arrays cannot be created directly; use new ArrayList() instead
D Generic arrays are valid in Java 8+
Correct Answer:  C. Generic arrays cannot be created directly; use new ArrayList() instead
EXPLANATION

You cannot create arrays of generic types due to type erasure and heap pollution risks. Use Collection framework instead of arrays.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips