Java Generics Quiz: Wildcards, PECS and Type Erasure Test
Practise Java generics with 14 questions and two programs on generic classes and methods, bounded types, wildcards and PECS, type erasure and raw types.
- Course: Java study plan
- Module: Generics
- Kind: Checkpoint — cleared at 70%
- Reading time: 24 min
- Runtime: OpenJDK 18
Checkpoint — Generics is the checkpoint that closes the Generics module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the purpose of generics, generic classes and methods, bounded type parameters, wildcards and PECS, type erasure and its consequences, and the practical idioms.
How it works. Fourteen questions and two programs; 70% on the questions and both programs accepted clears the module.
Before you start, make sure you can answer:
- Why
List<Integer>is not aList<Number>, and which wildcard fixes a read-only parameter. - What
<T extends Comparable<? super T>>means, word by word. - Which of
new T(),T[],instanceof List<String>,static Tare legal, and why not. - What PECS stands for and how to apply it to
copy(dest, src). - What a raw type does to type safety.
The programs are a generic bounded container with a PECS-correct method, and a generic utility set whose signatures the compiler must accept for several element types.
Common questions
What does <T extends Comparable<? super T>> mean, word by word?
T is a type parameter; extends gives it an upper bound; that bound says T implements Comparable of T or of some supertype of T. So T values can be compared with each other, even when compareTo is inherited from a parent class.
How does PECS apply to copy(dest, src)?
src produces the elements, so it takes List<? extends T>; dest consumes them, so it takes List<? super T>. That is the JDK's Collections.copy(List<? super T> dest, List<? extends T> src).
Which of new T(), new T[n], instanceof List<String> and a static T field are legal?
None of them. Erasure leaves no class to construct for new T() or new T[n], the JVM cannot see the String in List<String> at run time, and one class serves every T, so a static field cannot have type T.
Exercises
A bounded box with PECS methods
Write class MinMax<T extends Comparable<? super T>> that keeps the smallest and largest of the values offered to it: void offer(T value), void offerAll(Collection<? extends T> values), void drainTo(Collection<? super T> dest) (adds min then max, if any, and resets), and Optional<T> min()/max().
Read an integer n and n integers; offer them all through offerAll to a MinMax<Integer>; print min=<v> max=<v> (or empty if n is 0); drain into a List<Number> and print its contents with toString, then after drain=<min().isPresent()>.
Example: 3 then 5 -2 9 →
min=-2 max=9
[-2, 9]
after drain=falseGeneric utilities that compile for everything
Write three generic utilities: static <T> List<T> reversed(List<? extends T> xs) returning a new list; static <T, R> List<R> mapAll(List<? extends T> xs, Function<? super T, ? extends R> f); and static <K, V extends Comparable<? super V>> K argMax(Map<K, V> m) returning the key with the largest value (any one on ties; null for an empty map).
Read an integer n and n words. Print reversed(words), then mapAll(words, String::length), then argMax of a map from each word to its length (build it with a LinkedHashMap and print longest=<key>).
Example: 3 then pear fig banana →
[banana, fig, pear]
[4, 3, 6]
longest=bananaMerge intervals of any comparable type
Write record Interval<T extends Comparable<? super T>>(T lo, T hi) and static <T extends Comparable<? super T>> List<Interval<T>> merge(List<Interval<T>> xs) that sorts by lo and merges overlapping or touching intervals (next.lo <= current.hi). Read n integer intervals and m word intervals (inclusive ranges of words in natural order), merge each list with the same method, and print them as [lo, hi] space-separated on one line each, then merged=<count of integer intervals>+<count of word intervals>.
Example: 3 then 1 3, 2 6, 8 10, then 2 then a c, b d →
[1, 6] [8, 10]
[a, d]
merged=2+1In this module: Generics
← Generics in practice — idioms and error messages · The collections framework — the map and how to choose →