What is the primary advantage of using generics in Java?
Answer: A
Generics provide compile-time type checking, preventing ClassCastException and eliminating the need for explicit casting at runtime.
Q.22Medium
In the declaration 'List<? extends Number> list', what types can be added to this list at runtime?
Answer: C
Upper-bounded wildcards (? extends T) are used for reading data. Adding elements is unsafe because the compiler doesn't know the exact type.
Q.23Medium
Which statement correctly uses the lower-bounded wildcard in generics?
Answer: A
Lower-bounded wildcards (? super T) allow superclasses of T. This assignment is valid because ArrayList<Number> fits '? super Integer'.
Q.24Medium
Which generic declaration would allow storing both String and Integer in the same collection?
Answer: D
All three allow storing multiple types because they use unbounded or Object-level wildcards, though List<?> is most restrictive for additions.
Q.25Medium
What does the PECS principle stand for in generics context?
Answer: A
PECS guides when to use upper bounds (? extends) for producers/readers and lower bounds (? super) for consumers/writers.
Advertisement
Q.26Easy
Which of the following will compile successfully?
Answer: B
Generics only work with reference types. 'int' is a primitive type, so 'Integer' wrapper class must be used instead.
Q.27Medium
In a generic class 'class Box<T extends Comparable<T>>', what constraint is placed on T?
Answer: A
The bound 'T extends Comparable<T>' ensures T is comparable to itself, enabling type-safe comparison operations.
Q.28Easy
What is the output of this code snippet?
ArrayList<String> list = new ArrayList<>();
list.add("Java");
Object obj = list.get(0);
String str = (String) obj;
System.out.println(str.length());
Answer: A
The code compiles and runs correctly. 'Java' has 4 characters. The explicit cast is necessary because get() returns Object type.
Q.29Medium
Which generic wildcard usage is INCORRECT?
Answer: D
Nested wildcards like '? extends ? super T' are not allowed. Wildcards cannot be combined or nested.
Q.30Medium
What is the relationship between raw types and generics in Java?
Answer: A
Raw types (like 'List' without type parameters) are legacy and should be avoided as they bypass compile-time type safety.
Q.31Medium
Consider the method: public static <T> T getFirst(List<T> list) { return list.get(0); }
How would you call this method for a List<String>?
Answer: D
Type inference in Java allows the compiler to deduce T from the argument type, so both explicit and implicit type specification work.
Q.32Hard
What will happen when you try to create an array of generic types like 'new ArrayList<String>[10]'?
Answer: A
Arrays of generic types are not allowed in Java due to type erasure and heap pollution prevention.
Q.33Easy
In the interface declaration 'interface Pair<K, V>', how many type parameters does it have?
Answer: B
The interface has two type parameters: K and V, making it a generic interface that can be implemented with different type combinations.
Q.34Medium
What is the significance of the '&' operator in generic bounds like '<T extends A & B>'?
Answer: A
Multiple bounds (using &) mean T must satisfy all constraints. This is useful when a type needs to implement multiple interfaces.
Q.35Hard
Consider: Map<String, ? extends List<?>> map = new HashMap<>();
What can you safely retrieve from this map?
Answer: A
The value type is '? extends List<?>', so you get a List with unknown element type. The exact subtype is unknown at compile-time.
Q.36Medium
Which statement about generic type parameters is TRUE?
Answer: C
While convention uses single uppercase letters, type parameters can be any valid identifier. Statements B and D are also incorrect.
Q.37Hard
What is the key difference between '? extends T' and '? super T' in practical usage?
Answer: A
Upper bounds (? extends T) are safe for reading because the compiler knows it's at least T. Lower bounds (? super T) are safe for writing because you can pass T or any superclass.
Q.38Easy
Which of the following is a valid generic method declaration in Java?
Answer: A
Type parameters in Java must be declared before the return type using angle brackets. Option A has correct syntax: <T> comes before return type void.
Q.39Easy
What is the primary purpose of bounded type parameters in generics?
Answer: A
Bounded type parameters like <T extends Number> restrict the types that can be passed as type arguments, ensuring type safety and enabling access to specific methods.
Q.40Medium
Consider the declaration: List<? super Integer> list. What does this wildcard represent?
Answer: B
'? super Integer' is a lower bounded wildcard that accepts Integer and all its superclasses like Number, Object. This allows write operations with Integer values.