Java Programming — Generics
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 41–50 of 100 questions in Generics
Q.41 Medium Generics
Which of the following will cause a compile-time error?
A List list = new ArrayList();
B List list = new ArrayList();
C List
D List list = new ArrayList();
Correct Answer:  B. List list = new ArrayList();
EXPLANATION

Java does not allow covariance with generics. List<Integer> cannot be assigned to List<Number> because you could add a Double to the latter, breaking type safety.

Take Test
Q.42 Medium Generics
Consider the following code snippet:
public T processData(List
A T must be a subtype of the elements in the list
B The list can contain elements that are subtypes of T
C T and the wildcard bound are independent
D The list must contain exactly type T
Correct Answer:  B. The list can contain elements that are subtypes of T
EXPLANATION

The wildcard '? extends T' means the list can contain any type that is a subtype of T. This allows reading from the list safely but restricts writing.

Take Test
Q.43 Easy Generics
What is the result of type erasure in Java generics?
A Generic type information is preserved at runtime
B All generic type information is removed at compile time, replaced with raw types or bounds
C Generic types are converted to Object only
D Type erasure only applies to wildcards
Correct Answer:  B. All generic type information is removed at compile time, replaced with raw types or bounds
EXPLANATION

Type erasure removes all generic type parameters during compilation. For unbounded types, they're replaced with Object; for bounded types, they're replaced with the upper bound.

Take Test
Q.44 Easy Generics
Which of the following is a valid generic class declaration in Java?
A public class Box { }
B public class Box { }
C public class Box { }
D public class Box { }
Correct Answer:  A. public class Box { }
EXPLANATION

Valid generic class declarations allow multiple type parameters with upper bounds. Option A is syntactically correct. Option C uses invalid 'super' keyword in class declaration. Option D has malformed syntax.

Take Test
Q.45 Easy Generics
What is the advantage of using generics over raw types in terms of 2024-25 Java standards?
A Compile-time type checking prevents ClassCastException
B Enables code reuse without sacrificing type safety
C Improves API clarity and documentation
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

Generics provide compile-time safety, code reusability through parameterization, and clearer API contracts. These are essential for modern Java development standards.

Take Test
Advertisement
Q.46 Medium Generics
How would you define a generic method that works with a pair of objects and returns the first one?
A T getFirst(T first, U second) { return first; }
B T getFirst(T first, T second) { return first; }
C public T getFirst(T first, U second) { return first; }
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

Both A and C define valid generic methods with multiple type parameters. C is more explicit with 'public' modifier, but both are functionally equivalent.

Take Test
Q.47 Medium Generics
What is the output?

List strings = new ArrayList();
List list = strings;
list.add(123);
String str = strings.get(0);
System.out.println(str);
A 123
B Compilation error
C Runtime ClassCastException
D null
Correct Answer:  C. Runtime ClassCastException
EXPLANATION

Adding Integer 123 to raw type reference bypasses generics. When retrieving as String, a ClassCastException occurs at runtime.

Take Test
Q.48 Hard Generics
Which statement about generic constructors is TRUE?
A Generic constructors must be in generic classes only
B Non-generic classes can have generic constructors
C Constructor type parameters are independent of class type parameters
D Both B and C
Correct Answer:  D. Both B and C
EXPLANATION

Generic constructors can exist in non-generic classes. Constructor's type parameters are separate from and independent of the class's type parameters.

Take Test
Q.49 Hard Generics
What is the PECS principle in generics?
A Producer Extends, Consumer Super
B Public Extends, Consumer Sets
C Parameter Encapsulation and Class Structure
D Polymorphic Extension and Class Signatures
Correct Answer:  A. Producer Extends, Consumer Super
EXPLANATION

PECS (Producer Extends, Consumer Super) is a mnemonic for wildcard bounds: use '? extends' when reading (producing), use '? super' when writing (consuming).

Take Test
Q.50 Hard Generics
Can you create an instance of generic type parameter directly like: T obj = new T();?
A Yes, always possible
B No, because type information is erased at runtime
C Yes, only if T extends Serializable
D Only with reflection
Correct Answer:  B. No, because type information is erased at runtime
EXPLANATION

Due to type erasure, T is replaced with Object at runtime, and you cannot instantiate Object with new T(). You need reflection or pass Class<T> as parameter.

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