Java Programming — Generics
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 71–80 of 100 questions in Generics
Q.71 Medium 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.

Take Test
Q.72 Medium Generics
Which generic wildcard usage is INCORRECT?
A List
B List
C Map map;
D List
Correct Answer:  D. List
EXPLANATION

Nested wildcards like '? extends ? super T' are not allowed. Wildcards cannot be combined or nested.

Take Test
Q.73 Easy Generics
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
Correct Answer:  A. 4
EXPLANATION

The code compiles and runs correctly. 'Java' has 4 characters. The explicit cast is necessary because get() returns Object type.

Take Test
Q.74 Medium Generics
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.

Take Test
Q.75 Easy Generics
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.

Take Test
Q.76 Medium Generics
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.

Take Test
Q.77 Medium Generics
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.

Take Test
Q.78 Medium Generics
Which statement correctly uses the lower-bounded wildcard in generics?
A List
B List
C List list = new ArrayList();
D List list = new ArrayList();
Correct Answer:  A. List
EXPLANATION

Lower-bounded wildcards (? super T) allow superclasses of T. This assignment is valid because ArrayList<Number> fits '? super Integer'.

Take Test
Q.79 Medium Generics
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.

Take Test
Q.80 Easy Generics
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.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips