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?
Apublic class Response { private T data; private int statusCode; }
Bpublic class Response { private Object data; private int statusCode; }
Cpublic class Response { private T data; private int statusCode; }
Dpublic 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.
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?
Apublic static void addElements(List
Bpublic static void addElements(List destination, List source)
Cpublic static void addElements(List destination, List source)
Dpublic 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.
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.
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?
Apublic static void process(List
Bpublic static void process(List list)
Cpublic static void process(List
Dpublic 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.
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?
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.
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?
Apublic static double calculateSum(List numbers)
Bpublic static double calculateSum(List numbers)
Cpublic static double calculateSum(List numbers)
Dpublic 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.
With lower-bounded wildcard (? super Integer), you can add Integer but retrieval returns Object. Casting Object to Integer without proper type causes ClassCastException.