Consider this declaration: public static <T> List<T> createList(). Which statement about type inference is correct?
Answer: B
Java's type inference deduces T from the assignment context. E.g., 'List<String> list = createList()' infers T as String.
Q.782Hard
What is the difference between List<Object> and List<?> in practical usage?
Answer: C
List<Object> accepts Object and its subclasses for adding. List<?> (unbounded wildcard) is more flexible for reading but restrictive for writing (only null).
Q.783Hard
What is the compiled bytecode signature of this generic method?
public <T extends Comparable<T>> T findMin(T[] arr)
Answer: A
Type erasure replaces T with its bound Comparable. The method signature becomes public Comparable findMin(Comparable[] arr).
Q.784Hard
Which statement about generic array creation is correct?
Answer: C
You cannot create arrays of generic types due to type erasure and heap pollution risks. Use Collection framework instead of arrays.
Q.785Hard
What output will this produce?
java
List<? super Integer> list = new ArrayList<Number>();
list.add(5);
Integer val = (Integer) list.get(0);
Answer: C
With lower-bounded wildcard (? super Integer), you can add Integer but retrieval returns Object. Casting Object to Integer without proper type causes ClassCastException.
Advertisement
Q.786Hard
Which Java feature was introduced specifically to support generics compilation?
Answer: A
Bridge methods are synthetic methods generated during compilation to maintain polymorphism with generic types and type erasure.
Q.787Medium
What is the runtime class object for this generic variable?
java
List<String> list = new ArrayList<String>();
Class<?> c = list.getClass();
Answer: C
Due to type erasure, getClass() returns ArrayList without generic parameters. Generic information exists only at compile time.
Q.788Medium
A developer creates a generic method that accepts a collection of numbers and calculates their sum. Which type parameter declaration would be most appropriate for this scenario?
Answer: A
Using 'T extends Number' establishes an upper bound, allowing the method to work with any Number subclass (Integer, Double, Float, etc.) while providing type safety and access to Number methods.
Q.789Easy
In a Spring Boot application, you need to create a generic repository interface that works with any entity type. Which declaration correctly implements this pattern?
Answer: A
Option A uses single type parameter T for entity type, which is the standard pattern for generic repositories. Option C is redundant (all classes extend Object), and Option D adds unnecessary complexity.
Q.790Medium
A utility class needs a method that accepts a List containing elements that are instances of a specific class or its subclasses. Which wildcard notation should be used?
Answer: A
'? extends TargetClass' creates a covariant wildcard allowing lists of TargetClass or any subclass. This is ideal for reading operations when you need polymorphic list handling.
Q.791Easy
When using bounded type parameters with multiple interfaces, what is the correct syntax in Java?
Answer: A
Java uses ampersand (&) to combine multiple bounds. The class must appear first, followed by interfaces. Commas, pipes, and 'implements' keyword are not valid syntax for type parameter bounds.
Q.792Hard
A method processes collections where items need to be added back to the same collection type. Which wildcard approach ensures type safety for write operations?
Answer: A
'? super T' allows writing T instances (contravariant), while '? extends T' allows safe reading (covariant). This combination enables flexible yet type-safe collection manipulation.
Q.793Medium
In a microservices architecture, you're designing a Response wrapper class that should work with any data type, including primitives when boxed. Which implementation is correct?
Answer: A
Option A provides proper generic type safety without unnecessary constraints. While Option C adds a Serializable bound (sometimes desirable), Option A is the most flexible and commonly used pattern for generic response wrappers.
Q.794Easy
What is the primary purpose of lambda expressions introduced in Java 8?
Answer: A
Lambda expressions provide a concise way to implement single abstract method (functional interfaces) without verbose anonymous class syntax.
Q.795Easy
Which of the following is a valid lambda expression syntax in Java?
Answer: A
Valid Java lambda syntax uses arrow operator (->). Parameters can be typed or untyped, and the expression or block follows the arrow.
Q.796Easy
What is a functional interface in Java?
Answer: A
A functional interface has exactly one abstract method. It can have multiple default methods. The @FunctionalInterface annotation can be used to mark it.
Q.797Easy
Which package contains the built-in functional interfaces in Java?
Answer: A
The java.util.function package contains functional interfaces like Predicate, Consumer, Function, Supplier, and BiFunction introduced in Java 8.
Q.798Easy
What will be the output of the following code?
UnaryOperator<Integer> square = x -> x * x;
System.out.println(square.apply(5));
Answer: A
UnaryOperator applies a function on its argument and returns result of same type. square.apply(5) returns 5*5 = 25.
Q.799Medium
Which of the following lambda expressions is invalid?
Answer: D
Lambda must have parameter list (even if empty with ()). Missing parameter list '() -> x + y' is invalid syntax.
Q.800Medium
What is the type of the following lambda expression: x -> x.length()?
Answer: A
Function<T,R> takes one argument of type T and returns R. Here String -> Integer (length), so Function<String, Integer>.