1Z0-819 Generics & Type Inference 2 — Questions and Answers
Question 1: What is the PECS principle in Java generics?
- Producer Extends, Consumer Super (Correct answer)
- Producer Exceptions, Consumer Static
- Parameter Extends, Consumer Syntax
- Primitive Extends, Collection Super
Correct answer: Producer Extends, Consumer Super
PECS stands for Producer Extends, Consumer Super: use <? extends T> when reading (producing) from a structure, and <? super T> when writing (consuming) to a structure.
Question 2: Which operation is valid on a variable declared as List<? extends Number>?
- Adding an Integer element to the list
- Adding a Double element to the list
- Reading elements and treating them as Number type (Correct answer)
- Adding a Number element directly to the list
Correct answer: Reading elements and treating them as Number type
With an upper-bounded wildcard <? extends Number>, you can read elements as Number or its supertype, but cannot add any elements because the exact subtype is unknown to the compiler.
Question 3: Which operation is valid on a variable declared as List<? super Integer>?
- Reading elements as Integer with guaranteed compile-time type safety
- Adding an Integer or any of its subtypes to the list (Correct answer)
- Adding a String element because the bound accepts any supertype
- Reading elements as a specific Number subtype safely
Correct answer: Adding an Integer or any of its subtypes to the list
With a lower-bounded wildcard <? super Integer>, you can safely add Integer values (or subtypes) to the list, but reading only returns Object.
Question 4: Which of the following correctly declares a type parameter with multiple bounds?
- public class MyClass<T extends Serializable & Comparable<T>> (Correct answer)
- public class MyClass<T extends Serializable, Comparable<T>>
- public class MyClass<T implements Serializable & Comparable<T>>
- public class MyClass<T super Serializable & Comparable<T>>
Correct answer: public class MyClass<T extends Serializable & Comparable<T>>
Multiple bounds use the & separator between bounds, and all bounds must use the extends keyword — even for interfaces — as in <T extends Serializable & Comparable<T>>.
Question 5: Why does the following code NOT compile? List<Number> nums = new ArrayList<Integer>();
- ArrayList cannot be assigned to a List variable
- Integer must be explicitly cast to Number before assignment
- Generic types in Java are invariant, so List<Integer> is not a subtype of List<Number> (Correct answer)
- The diamond operator must be used on the right-hand side
Correct answer: Generic types in Java are invariant, so List<Integer> is not a subtype of List<Number>
Java generics are invariant, meaning List<Integer> is not a subtype of List<Number> even though Integer is a subtype of Number; this prevents heap pollution.
Question 6: What is the correct signature for Collections.sort() that handles types comparable to their supertypes?
- public static <T extends Comparable<T>> void sort(List<T> list)
- public static <T extends Comparable<? super T>> void sort(List<T> list) (Correct answer)
- public static <T super Comparable<T>> void sort(List<T> list)
- public static <T implements Comparable<T>> void sort(List<T> list)
Correct answer: public static <T extends Comparable<? super T>> void sort(List<T> list)
Collections.sort() uses <T extends Comparable<? super T>> to allow types whose compareTo method accepts a supertype, making it more flexible than the simpler <T extends Comparable<T>>.
Question 7: What is the result of calling getClass() on a List<String> and a List<Integer> at runtime?
- They return different class objects: the JVM tracks List<String>.class separately from List<Integer>.class
- They return the same class object because type erasure removes parameterized type information (Correct answer)
- getClass() throws an UnsupportedOperationException for generic types
- They both return Object.class because all generic type parameters erase to Object
Correct answer: They return the same class object because type erasure removes parameterized type information
Due to type erasure, both List<String> and List<Integer> share the same runtime class (e.g., java.util.ArrayList), so getClass() returns the same Class object.
What is the PECS principle in Java generics?