Showing 71–80 of 100 questions
in Generics
What is the relationship between raw types and generics in Java?
A
Raw types are legacy code that bypasses generic type checking
B
Raw types are automatically converted to generic types
C
Raw types and generics cannot coexist
D
Raw types are more efficient than generics
Correct Answer:
A. Raw types are legacy code that bypasses generic type checking
EXPLANATION
Raw types (like 'List' without type parameters) are legacy and should be avoided as they bypass compile-time type safety.
Which generic wildcard usage is INCORRECT?
A
List
B
List
C
Map map;
D
List
EXPLANATION
Nested wildcards like '? extends ? super T' are not allowed. Wildcards cannot be combined or nested.
What is the output of this code snippet?
ArrayList list = new ArrayList();
list.add("Java");
Object obj = list.get(0);
String str = (String) obj;
System.out.println(str.length());
A
4
B
Compilation error
C
Runtime ClassCastException
D
null
EXPLANATION
The code compiles and runs correctly. 'Java' has 4 characters. The explicit cast is necessary because get() returns Object type.
In a generic class 'class Box', what constraint is placed on T?
A
T must implement the Comparable interface with itself as the type parameter
B
T must be a subclass of Comparable
C
T must be Comparable with any type
D
T is unrestricted and Comparable is optional
Correct Answer:
A. T must implement the Comparable interface with itself as the type parameter
EXPLANATION
The bound 'T extends Comparable<T>' ensures T is comparable to itself, enabling type-safe comparison operations.
Which of the following will compile successfully?
A
List list = new ArrayList();
B
List list = new ArrayList();
C
List list = new ArrayList();
D
List list = new ArrayList();
Correct Answer:
B. List list = new ArrayList();
EXPLANATION
Generics only work with reference types. 'int' is a primitive type, so 'Integer' wrapper class must be used instead.
What does the PECS principle stand for in generics context?
A
Producer Extends, Consumer Super
B
Parameter Extends, Class Super
C
Polymorphic Extends, Constructor Super
D
Public Extends, Static Super
Correct Answer:
A. Producer Extends, Consumer Super
EXPLANATION
PECS guides when to use upper bounds (? extends) for producers/readers and lower bounds (? super) for consumers/writers.
Which generic declaration would allow storing both String and Integer in the same collection?
A
List list = new ArrayList();
B
List list = new ArrayList();
C
List
D
All of the above
Correct Answer:
D. All of the above
EXPLANATION
All three allow storing multiple types because they use unbounded or Object-level wildcards, though List<?> is most restrictive for additions.
Which statement correctly uses the lower-bounded wildcard in generics?
A
List
B
List
C
List list = new ArrayList();
D
List list = new ArrayList();
EXPLANATION
Lower-bounded wildcards (? super T) allow superclasses of T. This assignment is valid because ArrayList<Number> fits '? super Integer'.
In the declaration 'List
A
Any Number subclass
B
Only Number or its exact subclasses
C
Nothing can be added safely (except null)
D
Only Integer and Double
Correct Answer:
C. Nothing can be added safely (except null)
EXPLANATION
Upper-bounded wildcards (? extends T) are used for reading data. Adding elements is unsafe because the compiler doesn't know the exact type.
What is the primary advantage of using generics in Java?
A
Type safety and elimination of explicit casting
B
Increased execution speed
C
Reduced memory consumption
D
Automatic parallel processing
Correct Answer:
A. Type safety and elimination of explicit casting
EXPLANATION
Generics provide compile-time type checking, preventing ClassCastException and eliminating the need for explicit casting at runtime.