Generics & Type Inference Flashcards
7 cards from real 1Z0-819 practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Generics & Type Inference flashcards as text
What is the var keyword available in Java 10 and Java 11?
Answer: A keyword for local variable type inference that lets the compiler determine the type
var is a reserved type name for local variable type inference; the compiler determines the variable's type from the right-hand side initializer expression at compile time.
Which of the following var declarations is INVALID in Java 11?
Answer: var x;
var requires an initializer expression so the compiler can infer the type; declaring var x; without an initializer is not allowed because the type cannot be determined.
Where can the var keyword NOT be used in Java 11?
Answer: As the declared type of an instance field in a class
var can only be used for local variable type inference; it cannot be used for instance fields, static fields, method parameters, or method return types.
Why can you NOT instantiate a generic type parameter with new T() in Java?
Answer: Due to type erasure, T is not available at runtime so the JVM cannot know which class to instantiate
Because of type erasure, T is erased to Object (or its upper bound) in bytecode, so the JVM has no information about the actual class T represents when new T() would be executed.
Which of the following correctly relies on type inference for a generic method call?
Answer: List list = Collections.emptyList();
The compiler infers the type argument for Collections.emptyList() from the target type List on the left-hand side, so no explicit type witness is needed.
At runtime, how can you check if an object is specifically a List?
Answer: You cannot check for the parameterized type at runtime due to type erasure
Due to type erasure, generic type parameters are not available at runtime, making instanceof List a compile error; you can only check instanceof List.
What is the inferred type of the variable 'map' in: var map = new HashMap>();?
Answer: HashMap>
var infers the exact static type of the right-hand side expression, which is HashMap> — the concrete class, not the Map interface.