Consider the code:
List<? extends Number> list = new ArrayList<>();
list.add(5); // Will this compile?
Answer: A
Upper bounded wildcards (? extends Number) are read-only. You cannot add elements because the compiler doesn't know the exact subtype.
Q.702Medium
Which of the following represents a bounded type parameter?
Answer: A
Bounded type parameters restrict the types that can be used as arguments. T extends Number ensures T is Number or its subclass.
Q.703Medium
What is the difference between List<?> and List<Object>?
Answer: A
List<?> accepts any type but is read-only (can't add). List<Object> explicitly accepts Object type and allows adding objects.
Q.704Medium
Consider this generic interface:
interface Comparable<T> { int compareTo(T o); }
Which class definition correctly implements this?
Answer: A
Option A correctly implements the generic interface by specifying the concrete type parameter as MyClass in the implements clause.
Q.705Medium
What will be the output?
List<Integer> list = new ArrayList<>();
list.add(10);
Object obj = list.get(0);
System.out.println(obj instanceof Integer);
Answer: A
The Integer value is stored in the list. At runtime, type erasure converts it to Object, but the actual stored value remains an Integer instance.
Advertisement
Q.706Hard
Which of these correctly demonstrates the Producer Extends Consumer Super (PECS) principle?
Answer: A
PECS principle: use 'extends' when reading from a collection (source), use 'super' when writing to it (dest). Option A reads from source and writes to dest correctly.
Q.707Hard
What will happen with this code?
List list = new ArrayList<String>();
list.add(123); // Adding Integer
String s = (String) list.get(0);
Answer: A
Raw type List accepts any object. At runtime, the Integer 123 cannot be cast to String, causing ClassCastException.
Q.708Hard
Consider this code:
public <T extends Comparable<T>> T getMax(T a, T b) {
return a.compareTo(b) > 0 ? a : b;
}
What is the benefit of this recursive bound <T extends Comparable<T>>?
Answer: A
Recursive bound <T extends Comparable<T>> ensures that T implements Comparable interface specifically for comparing with its own type, providing type safety.
Q.709Easy
What is the output of this generic code?
class Pair<K, V> {
public void display(K key, V value) {
System.out.println(key + ": " + value);
}
}
Pair<String, Integer> p = new Pair<>();
p.display("Age", 25);
Answer: A
The generic class Pair with two type parameters K and V correctly accepts String and Integer. The display method prints the key-value pair.
Q.710Hard
Which statement is true about generic inheritance?
Answer: B
ArrayList<Integer> is a subtype of List<Integer> because ArrayList is a subclass of List with the same type parameter.
Q.711Hard
What will be the result of this code?
List<String> list = new ArrayList<>();
list.add("Hello");
List raw = list; // Unchecked assignment
raw.add(123); // Adding Integer to raw type
String s = list.get(1);
Answer: A
Raw type assignment bypasses generics. Integer 123 is added to the list. When casting to String at get(1), ClassCastException occurs.
Q.712Medium
Which of these declarations would NOT cause a compilation warning or error?
Answer: A
Option A uses proper diamond syntax with matching type parameters on both sides. Options B, C produce unchecked warnings, and Option D is invalid due to invariance.
Q.713Easy
In Java generics, what does the wildcard '?' represent?
Answer: A
The wildcard '?' in generics represents an unknown type, allowing flexibility when the exact type is not known or not important.
Q.714Easy
What is the primary advantage of using generics in Java?
Answer: A
Generics provide compile-time type checking, preventing ClassCastException and eliminating the need for explicit casting at runtime.
Q.715Medium
In the declaration 'List<? extends Number> list', what types can be added to this list at runtime?
Answer: C
Upper-bounded wildcards (? extends T) are used for reading data. Adding elements is unsafe because the compiler doesn't know the exact type.
Q.716Medium
Which statement correctly uses the lower-bounded wildcard in generics?
Answer: A
Lower-bounded wildcards (? super T) allow superclasses of T. This assignment is valid because ArrayList<Number> fits '? super Integer'.
Q.717Medium
Which generic declaration would allow storing both String and Integer in the same collection?
Answer: D
All three allow storing multiple types because they use unbounded or Object-level wildcards, though List<?> is most restrictive for additions.
Q.718Medium
What does the PECS principle stand for in generics context?
Answer: A
PECS guides when to use upper bounds (? extends) for producers/readers and lower bounds (? super) for consumers/writers.
Q.719Easy
Which of the following will compile successfully?
Answer: B
Generics only work with reference types. 'int' is a primitive type, so 'Integer' wrapper class must be used instead.
Q.720Medium
In a generic class 'class Box<T extends Comparable<T>>', what constraint is placed on T?
Answer: A
The bound 'T extends Comparable<T>' ensures T is comparable to itself, enabling type-safe comparison operations.