Java Programming — Generics
Java OOP, collections, multithreading
49 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 49 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 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.3 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.4 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.5 Medium Generics
Which of these is a valid generic interface implementation?
A class MyClass implements Comparable
B class MyClass implements Comparable
C class MyClass extends Comparable
D class MyClass implements Comparable
Correct Answer:  A. class MyClass implements Comparable
EXPLANATION

Implementing generic interfaces requires specifying the type parameter. 'implements Comparable<MyClass>' is the proper way to implement.

Take Test
Advertisement
Q.6 Medium Generics
What is the PECS principle in Java Generics?
A Public Extends Collections Structure
B Producer Extends, Consumer Super
C Parameter Extends Comparable Supports
D Polymorphic Extends Class Super
Correct Answer:  B. Producer Extends, Consumer Super
EXPLANATION

PECS (Producer Extends, Consumer Super) is a guideline: use 'extends' for producers (reading) and 'super' for consumers (writing).

Take Test
Q.7 Medium Generics
What does 'covariance' mean in the context of Java Generics?
A Types vary together in the same direction
B Types vary in opposite directions
C Types do not vary at all
D Types are interchangeable
Correct Answer:  A. Types vary together in the same direction
EXPLANATION

Covariance means if Dog is a subtype of Animal, then Producer<Dog> is a subtype of Producer<Animal>. Achieved using upper-bounded wildcards.

Take Test
Q.8 Medium Generics
Which wildcard usage follows the Producer pattern?
A List
B List
C List
D List
Correct Answer:  B. List
EXPLANATION

Producer (Get-Only pattern) uses upper-bounded wildcard (? extends T). It's used when you want to read/produce elements of specific types.

Take Test
Q.9 Medium Generics
What is the correct output of this code?
java
List
A Compilation successful
B Compilation error
C Runtime exception
D Adds 5 to the list
Correct Answer:  B. Compilation error
EXPLANATION

With upper-bounded wildcard (? extends Number), you can read but cannot add elements (except null) because the compiler doesn't know the exact type.

Take Test
Q.10 Medium Generics
Consider the code:
java
List list = new ArrayList();

Will this compile?
A Yes, Integer is a subtype of Number
B No, ArrayList is not compatible with List
C Yes, with warning
D Depends on compiler version
Correct Answer:  B. No, ArrayList is not compatible with List
EXPLANATION

Generics are invariant. ArrayList<Integer> cannot be assigned to List<Number> even though Integer is a subtype of Number. This prevents type safety issues.

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