Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 191–200 of 958
Topics in Java Programming
Q.191 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.192 Easy Generics
What does the wildcard in List represent?
A Any type, with full read and write capabilities
B Any type, but with restricted capabilities - can only read as Object
C Unknown type that extends Object
D A placeholder for generic type parameter
Correct Answer:  B. Any type, but with restricted capabilities - can only read as Object
EXPLANATION

List<?> represents unknown type with read-only capabilities. Elements can only be read as Object. You cannot add elements (except null) due to type safety.

Take Test
Q.193 Medium Generics
Consider:
public class Pair { }
Which declaration is invalid?
A Pair pair = new Pair();
B Pair pair = new Pair();
C Pair pair = new Pair();
D Pair
Correct Answer:  D. Pair
EXPLANATION

Option D is invalid because we cannot instantiate a generic class with wildcard type parameters. Wildcards are only for variable declarations, not for instantiation with 'new'.

Take Test
Q.194 Easy Generics
What is the compiler-inferred type parameter in this call?
List list = Arrays.asList("a", "b", "c");
A T is inferred as Object
B T is inferred as String
C T is inferred as ? extends String
D T is inferred as ? super String
Correct Answer:  B. T is inferred as String
EXPLANATION

The compiler infers the most specific common type of the arguments. Since all arguments are String literals, T is inferred as String.

Take Test
Q.195 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.196 Medium Generics
What is the output of this code?
List strings = new ArrayList();
List raw = strings;
raw.add(123);
String str = strings.get(0);
A Compiles and runs without error
B Compile-time error: cannot add Integer to List
C Runtime ClassCastException when retrieving the element
D Compile-time error: raw type cannot be assigned
Correct Answer:  C. Runtime ClassCastException when retrieving the element
EXPLANATION

The code compiles (with warnings about raw types) but throws ClassCastException at runtime when trying to cast Integer to String. Type safety is lost with raw types.

Take Test
Q.197 Medium Generics
Given: Map
A map.put("key", new ArrayList())
B List list = map.get("key"); list.add("value");
C List list = map.get("key");
D map.put("key", Arrays.asList(1, 2, 3))
Correct Answer:  C. List list = map.get("key");
EXPLANATION

We can read from map.get() and assign to List<?>, but cannot add to List<?> or put into the map due to wildcard restrictions. Option C only reads the value.

Take Test
Q.198 Medium Generics
What does the following generic method signature indicate?
public static T findMax(T[] array)
A T must implement Comparable interface parameterized with itself
B T must be a Comparable type but not necessarily of itself
C Comparable is optional for T
D T is comparable only with Object types
Correct Answer:  A. T must implement Comparable interface parameterized with itself
EXPLANATION

The bounded type parameter 'T extends Comparable<T>' enforces that T must implement Comparable and be comparable with its own type. This is the F-bounded polymorphism pattern.

Take Test
Q.199 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.200 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
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