Consider the code:
java
List<Number> list = new ArrayList<Integer>();
Will this compile?
Answer: B
Generics are invariant. ArrayList<Integer> cannot be assigned to List<Number> even though Integer is a subtype of Number. This prevents type safety issues.
Q.82Medium
What is the correct output of this code?
java
List<? extends Number> list = new ArrayList<Integer>();
list.add(5);
Answer: B
With upper-bounded wildcard (? extends Number), you can read but cannot add elements (except null) because the compiler doesn't know the exact type.
Q.83Medium
Which wildcard usage follows the Producer pattern?
Answer: B
Producer (Get-Only pattern) uses upper-bounded wildcard (? extends T). It's used when you want to read/produce elements of specific types.
Q.84Medium
What does 'covariance' mean in the context of Java Generics?
Answer: A
Covariance means if Dog is a subtype of Animal, then Producer<Dog> is a subtype of Producer<Animal>. Achieved using upper-bounded wildcards.
Q.85Medium
What is the PECS principle in Java Generics?
Answer: B
PECS (Producer Extends, Consumer Super) is a guideline: use 'extends' for producers (reading) and 'super' for consumers (writing).
Advertisement
Q.86Medium
Which of these is a valid generic interface implementation?
Answer: A
Implementing generic interfaces requires specifying the type parameter. 'implements Comparable<MyClass>' is the proper way to implement.
Q.87Hard
What will happen when you compile and run this code?
java
List<String> strings = Arrays.asList("a", "b", "c");
List raw = strings;
raw.add(123);
String s = strings.get(3);
Answer: C
Assigning generic type to raw type suppresses type checking. The Integer added bypasses the type system, causing ClassCastException when retrieved as String.
Q.88Hard
Consider this declaration: public static <T> List<T> createList(). Which statement about type inference is correct?
Answer: B
Java's type inference deduces T from the assignment context. E.g., 'List<String> list = createList()' infers T as String.
Q.89Hard
What is the difference between List<Object> and List<?> in practical usage?
Answer: C
List<Object> accepts Object and its subclasses for adding. List<?> (unbounded wildcard) is more flexible for reading but restrictive for writing (only null).
Q.90Hard
What is the compiled bytecode signature of this generic method?
public <T extends Comparable<T>> T findMin(T[] arr)
Answer: A
Type erasure replaces T with its bound Comparable. The method signature becomes public Comparable findMin(Comparable[] arr).
Q.91Hard
Which statement about generic array creation is correct?
Answer: C
You cannot create arrays of generic types due to type erasure and heap pollution risks. Use Collection framework instead of arrays.
Q.92Hard
What output will this produce?
java
List<? super Integer> list = new ArrayList<Number>();
list.add(5);
Integer val = (Integer) list.get(0);
Answer: C
With lower-bounded wildcard (? super Integer), you can add Integer but retrieval returns Object. Casting Object to Integer without proper type causes ClassCastException.
Q.93Hard
Which Java feature was introduced specifically to support generics compilation?
Answer: A
Bridge methods are synthetic methods generated during compilation to maintain polymorphism with generic types and type erasure.
Q.94Medium
What is the runtime class object for this generic variable?
java
List<String> list = new ArrayList<String>();
Class<?> c = list.getClass();
Answer: C
Due to type erasure, getClass() returns ArrayList without generic parameters. Generic information exists only at compile time.
Q.95Medium
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?
Answer: A
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.
Q.96Easy
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?
Answer: A
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.
Q.97Medium
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?
Answer: A
'? 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.
Q.98Easy
When using bounded type parameters with multiple interfaces, what is the correct syntax in Java?
Answer: A
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.
Q.99Hard
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?
Answer: A
'? super T' allows writing T instances (contravariant), while '? extends T' allows safe reading (covariant). This combination enables flexible yet type-safe collection manipulation.
Q.100Medium
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?
Answer: A
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.