1Z0-819 Generics & Type Inference 1 — Questions and Answers
Question 1: What does type erasure mean in the context of Java generics?
- Generic type parameters are converted to their bounds (or Object) in compiled bytecode (Correct answer)
- Generic type information is retained at runtime for reflection purposes
- Generic types are validated at runtime by the JVM
- Type erasure removes all Object references from generic classes
Correct answer: Generic type parameters are converted to their bounds (or Object) in compiled bytecode
Type erasure means the compiler removes generic type information during compilation, replacing type parameters with their bounds or Object in the bytecode to maintain backward compatibility.
Question 2: Which of the following correctly declares a generic method in Java?
- public T <T> getValue() { return null; }
- public <T> T getValue() { return null; } (Correct answer)
- public generic<T> T getValue() { return null; }
- public T getValue<T>() { return null; }
Correct answer: public <T> T getValue() { return null; }
A generic method must declare its type parameter(s) in angle brackets before the return type, as in public <T> T getValue().
Question 3: What does the diamond operator <> accomplish when used in Java?
- Declares an unbounded wildcard type for any collection
- Creates an empty generic collection with no element type
- Instructs the compiler to infer the type parameter from the surrounding context (Correct answer)
- Specifies that a class implements a generic interface
Correct answer: Instructs the compiler to infer the type parameter from the surrounding context
The diamond operator <> tells the compiler to infer the generic type argument from the left-hand side or context, reducing verbosity in object creation expressions.
Question 4: Which statement about using raw types in Java is TRUE?
- Raw types are fully type-safe because they default to Object
- Using raw types generates compile-time errors in modern Java
- Raw types bypass generic type checking and can lead to ClassCastException at runtime (Correct answer)
- Raw types are recommended when writing new code for maximum flexibility
Correct answer: Raw types bypass generic type checking and can lead to ClassCastException at runtime
Raw types bypass the compiler's generic type safety checks, potentially leading to ClassCastException at runtime when incompatible types are inserted or retrieved.
Question 5: Given: List<String> strList = new ArrayList<>(); Which assignment is valid?
- List<Object> objList = strList;
- List<? extends String> extList = strList; (Correct answer)
- ArrayList<Object> arrList = strList;
- Collection<Object> colList = strList;
Correct answer: List<? extends String> extList = strList;
List<? extends String> accepts a List<String> because it is an upper-bounded wildcard; generic types are invariant, so all Object-based assignments are compile errors.
Question 6: Which of the following creates an illegal parameterized array in Java?
- Object[] arr = new String[5];
- List<?>[] arr = new ArrayList<?>[5];
- List<String>[] arr = new ArrayList<String>[5]; (Correct answer)
- String[] arr = new String[5];
Correct answer: List<String>[] arr = new ArrayList<String>[5];
Java does not allow creation of arrays of parameterized types such as new ArrayList<String>[5] because it would undermine type safety due to array covariance.
Question 7: What is the primary benefit of using generics over using Object as a parameter type?
- Generics allow the use of primitive types directly without autoboxing
- Generics provide compile-time type safety and eliminate the need for explicit casting (Correct answer)
- Generics improve runtime performance by skipping JVM type checks entirely
- Generics allow creating instances of type parameters using new T()
Correct answer: Generics provide compile-time type safety and eliminate the need for explicit casting
Generics enforce type safety at compile time, removing the need for explicit casts and preventing ClassCastException that could occur when using raw Object-based collections.
What does type erasure mean in the context of Java generics?