Home Subjects Java Programming Generics

Java Programming
Generics

Java OOP, collections, multithreading

49 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–30 of 49
Topics in Java Programming
Q.21 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.

Test
Q.22 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.

Test
Q.23 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.

Test
Q.24 Medium Generics
What happens when you use raw type List instead of List?
A Type safety is lost and unchecked warnings appear
B All elements are treated as Object
C Runtime ClassCastException may occur
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

Using raw types bypasses generic type checking, losing type safety. Elements are treated as Object at runtime, potentially causing ClassCastException when casting.

Test
Q.25 Medium Generics
Consider a generic interface: interface Comparable { int compareTo(T obj); }. How should a class implement this for String comparison?
A class MyClass implements Comparable { }
B class MyClass implements Comparable { }
C class MyClass implements Comparable { }
D Both B and C are valid
Correct Answer:  D. Both B and C are valid
EXPLANATION

Option B directly specifies String, while Option C makes the class generic. Both are valid approaches to implement a generic interface.

Test
Q.26 Medium Generics
Which of the following will NOT compile?
A List numbers = new ArrayList();
B List
C List
D List objects = new ArrayList();
Correct Answer:  D. List objects = new ArrayList();
EXPLANATION

Option D fails because List<Object> cannot reference ArrayList<String>. Generics are invariant. Options A, B, C are valid (covariance with extends, contravariance with super).

Test
Q.27 Medium Generics
What does List represent in Java generics?
A List of unknown type
B List of any type
C List that can hold any single unknown type
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

List<?> is an unbounded wildcard that represents a list of unknown type. You can read from it (get Object), but cannot write to it (except null). Both A, B, and C are correct interpretations.

Test
Q.28 Medium Generics
Consider: public static T findMax(T a, T b) { return a; }. What is the issue with calling findMax(5, 3.5)?
A T cannot be inferred because int and double are different
B The method will compile and run without issues
C T will be Object type
D Runtime exception will occur
Correct Answer:  A. T cannot be inferred because int and double are different
EXPLANATION

The type parameter T cannot simultaneously be int and double. The compiler cannot infer a single type T that satisfies both arguments, resulting in a compilation error.

Test
Q.29 Medium Generics
Which statement correctly defines a generic class with multiple type parameters and bounds?
A class Container { }
B class Container extends Number { }
C class Container { }
D class Container extends { }
Correct Answer:  C. class Container { }
EXPLANATION

Multiple type parameters with bounds must be comma-separated and each can have its own upper bound. Option C shows correct syntax with recursive bound on T and bound on K.

Test
Q.30 Medium Generics
Consider the declaration: List
A List of Integer and its subclasses
B List of Integer and its superclasses
C List of any type
D List of Number and Integer only
Correct Answer:  B. List of Integer and its superclasses
EXPLANATION

'? super Integer' is a lower bounded wildcard that accepts Integer and all its superclasses like Number, Object. This allows write operations with Integer values.

Test
IGET
IGET AI
Online · Exam prep assistant
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