Which generic wildcard usage follows the consumer pattern correctly?
Answer: B
Consumer pattern uses '? super T' (lower bound). This allows adding elements safely. Option A (producer pattern) only allows reading, and Options C and D restrict type flexibility.
Q.762Hard
What is the compiled signature of this generic method?
public <T extends Number & Comparable<T>> void sort(T[] array)
Answer: D
With multiple bounds using '&', type erasure replaces T with the first bound. Here, Number is the first bound, so T is replaced with Number.
Q.763Medium
What is the correct way to declare a generic method that accepts a list and returns the maximum element?
Answer: D
Option D correctly ensures T is comparable with itself using F-bounded polymorphism. Option C has inverted logic with '? super'. Option B is unnecessarily complex.
Q.764Medium
Given the declaration: List<List<String>> listOfLists;
Which assignment is valid?
Answer: D
Nested generics must match exactly or use appropriate wildcards. Only option D provides exact type match. Option B would work with wildcards, but that's not listed correctly here.
Q.765Medium
What happens when you try to cast a generic object?
List<String> strings = (List<String>) getSomeList();
Answer: B
Due to type erasure, casts involving generic types are unchecked. The compiler warns about this but allows it. The actual cast check only verifies the raw type.
Advertisement
Q.766Hard
Which of the following represents valid bounded wildcard usage for a method that processes collections?
Answer: B
Only option B allows adding elements safely. '? super Integer' means the collection can hold Integer or any supertype. Option A prevents adding, Option C prevents adding, Option D restricts to exactly Number.
Q.767Easy
What is the relationship between generics and arrays in Java?
Answer: B
Java does not allow creation of arrays with parameterized types due to type erasure. List<String>[] would be unsafe. You must use List<String>[] or List[] instead.
Q.768Medium
In the context of generics, what does 'invariance' mean?
Answer: B
Generics are invariant in Java. List<Integer> is NOT a subtype of List<Number> to maintain type safety. This is a key feature preventing runtime errors.
Q.769Hard
How would you correctly use generics in a recursive type bound scenario?
Answer: A
Option A demonstrates recursive type bound (also called F-bounded polymorphism), which allows a type to reference itself in its bound. This is commonly used in builder patterns and comparators.
Q.770Easy
What will be the output of the following code?
java
List<String> list = new ArrayList<String>();
list.add("Java");
Object obj = list;
System.out.println(list.getClass() == obj.getClass());
Answer: A
Generic type information is erased at runtime. Both list and obj refer to the same ArrayList class, so getClass() returns the same class object.
Q.771Easy
Which of the following correctly declares a generic method that returns the first element of any collection?
Answer: B
Generic methods require type parameter declaration before return type. The collection parameter must be of type Collection<T> to extract element of type T.
Q.772Easy
Which declaration is valid in Java Generics?
Answer: B
Generics only work with reference types, not primitive types. Integer is the wrapper class for int, making option B valid.
Q.773Medium
What does the following generic declaration mean?
public <T extends Number & Comparable<T>> T findMax(T a, T b)
Answer: B
Multiple bounds in generics use '&' to specify that the type must satisfy all constraints. T must extend Number AND implement Comparable<T>.
Q.774Medium
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.775Medium
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.776Medium
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.777Medium
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.778Medium
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).
Q.779Medium
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.780Hard
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.