Consider the code:
java
List<Number> list = new ArrayList<Integer>();
Will this compile?
Answer: B
Generics are invariant. ArrayList<Integer> cannot be assigned to List<Number> even though Integer is a subtype of Number. This prevents type safety issues.
Q.382Medium
What is the correct output of this code?
java
List<? extends Number> list = new ArrayList<Integer>();
list.add(5);
Answer: B
With upper-bounded wildcard (? extends Number), you can read but cannot add elements (except null) because the compiler doesn't know the exact type.
Q.383Medium
Which wildcard usage follows the Producer pattern?
Answer: B
Producer (Get-Only pattern) uses upper-bounded wildcard (? extends T). It's used when you want to read/produce elements of specific types.
Q.384Medium
What does 'covariance' mean in the context of Java Generics?
Answer: A
Covariance means if Dog is a subtype of Animal, then Producer<Dog> is a subtype of Producer<Animal>. Achieved using upper-bounded wildcards.
Q.385Medium
What is the PECS principle in Java Generics?
Answer: B
PECS (Producer Extends, Consumer Super) is a guideline: use 'extends' for producers (reading) and 'super' for consumers (writing).
Advertisement
Q.386Medium
Which of these is a valid generic interface implementation?
Answer: A
Implementing generic interfaces requires specifying the type parameter. 'implements Comparable<MyClass>' is the proper way to implement.
Q.387Medium
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.388Medium
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.389Medium
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.390Medium
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.391Medium
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.392Medium
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>.
Q.393Medium
Which functional interface should be used for a lambda that filters elements?
Answer: A
Predicate<T> tests a condition and returns boolean. It's ideal for filtering. Function would return any type, Consumer doesn't return anything.
Q.394Medium
What will this code print?
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
list.forEach(x -> System.out.print(x + " "));
Answer: B
forEach with Consumer lambda prints each element followed by space, resulting in '1 2 3 4 5 ' (with trailing space).
Q.395Medium
Can lambda expressions access local variables from their enclosing scope?
Answer: A
Lambda expressions can access local variables only if they are final or effectively final (not modified after initialization) due to closure requirements.
Q.396Medium
Which of the following correctly represents a BiFunction?
Answer: A
BiFunction takes 2 parameters and returns a result. (int a, int b) -> a + b fits this. Others are Function, Supplier, Consumer respectively.
Q.397Medium
What is the difference between a lambda expression and an anonymous inner class?
Answer: D
Lambdas provide concise syntax, are compiled directly without separate class files, and work only with functional interfaces (single abstract method).
Q.398Medium
Which of the following statements about @FunctionalInterface is true?
Answer: B
@FunctionalInterface is optional annotation that helps compiler verify interface has exactly one abstract method, catching errors early.
Q.399Medium
Can a lambda expression have multiple statements in its body?
Answer: B
Lambda can have multiple statements using braces: (x,y) -> { int z = x+y; return z; }. Single expression needs no braces or return.
Q.400Medium
Which functional interface is best suited for this scenario: method that takes no parameters and returns a random number?
Answer: A
Supplier<T> takes no parameters () and returns T. Perfect for generating/providing values like random numbers.