Java Programming
Java OOP, collections, multithreading
212 Questions 10 Topics Take Test
Advertisement
Showing 41–50 of 212 questions
Q.41 Hard Generics
Which generic wildcard usage follows the consumer pattern correctly?
A public void consume(List
B public void consume(List
C public void consume(List list) { list.add(new Integer(5)); }
D public void consume(List list) { list.add(new Integer(5)); }
Correct Answer:  B. public void consume(List
EXPLANATION

Consumer pattern uses '? super T' (lower bound). This allows adding elements safely. Option A (producer pattern) only allows reading, and Options C and D restrict type flexibility.

Take Test
Q.42 Hard Generics
Which statement correctly implements a generic factory method?
A public static T create(Class clazz) { return new T(); }
B public static T create(Class clazz) throws Exception { return clazz.newInstance(); }
C public static T create(T t) { return (T) new Object(); }
D public T create() { return new T(); }
Correct Answer:  B. public static T create(Class clazz) throws Exception { return clazz.newInstance(); }
EXPLANATION

Cannot instantiate generic type T directly. Using Class<T>.newInstance() is a valid pattern (though deprecated in newer Java versions in favor of getConstructor()). Option A is invalid.

Take Test
Q.43 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.44 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.45 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
Advertisement
Q.46 Hard Generics
What is the difference between and ?
A No difference; both are equivalent
B First ensures T is comparable with itself; second is a raw type
C First is deprecated in Java 8+
D Second provides better type safety
Correct Answer:  B. First ensures T is comparable with itself; second is a raw type
EXPLANATION

<T extends Comparable<T>> is a recursive bound ensuring T implements Comparable of its own type. <T extends Comparable> uses raw type, losing type information.

Take Test
Q.47 Hard Generics
Which generic declaration allows a method to accept List of any type?
A public void process(List rawList) { }
B public void process(List list) { }
C public void process(List list) { }
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

All three approaches allow processing lists of any type, though they differ: raw type (unsafe), unbounded wildcard (read-only), and type parameter (flexible).

Take Test
Q.48 Hard Generics
What is the key difference between '? extends T' and '? super T' in practical usage?
A Extends is for reading data safely, Super is for writing data safely
B Extends is for writing, Super is for reading
C They have no practical difference
D Extends is for classes, Super is for interfaces
Correct Answer:  A. Extends is for reading data safely, Super is for writing data safely
EXPLANATION

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.

Take Test
Q.49 Hard Generics
Consider: Map
A A List of unknown element type
B A List
C Any List type
D Nothing can be safely retrieved
Correct Answer:  A. A List of unknown element type
EXPLANATION

The value type is '? extends List<?>', so you get a List with unknown element type. The exact subtype is unknown at compile-time.

Take Test
Q.50 Hard Generics
What will happen when you try to create an array of generic types like 'new ArrayList[10]'?
A Compilation error because arrays of generic types are not allowed
B Runtime error after successful compilation
C It will compile and run successfully
D Warning but no compilation error
Correct Answer:  A. Compilation error because arrays of generic types are not allowed
EXPLANATION

Arrays of generic types are not allowed in Java due to type erasure and heap pollution prevention.

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