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 11–20 of 49
Topics in Java Programming
Q.11 Medium Generics
What does the following generic declaration mean?
public T findMax(T a, T b)
A T must be Number or Comparable
B T must extend Number and implement Comparable
C T must be Number and a subclass of Comparable
D T can be either Number or Comparable
Correct Answer:  B. T must extend Number and implement Comparable
EXPLANATION

Multiple bounds in generics use '&' to specify that the type must satisfy all constraints. T must extend Number AND implement Comparable<T>.

Test
Q.12 Medium Generics
In the context of generics, what does 'invariance' mean?
A A generic type List is a subtype of List
B A generic type List is not a subtype of List, despite Integer being a subtype of Number
C Invariance allows covariance and contravariance together
D Invariance means the type cannot change
Correct Answer:  B. A generic type List is not a subtype of List, despite Integer being a subtype of Number
EXPLANATION

Generics are invariant in Java. List<Integer> is NOT a subtype of List<Number> to maintain type safety. This is a key feature preventing runtime errors.

Test
Q.13 Medium Generics
What happens when you try to cast a generic object?
List strings = (List) getSomeList();
A The cast is fully checked at compile time
B The cast is unchecked; generic type information is lost due to erasure
C The cast always succeeds because of type erasure
D A compile-time error is raised
Correct Answer:  B. The cast is unchecked; generic type information is lost due to erasure
EXPLANATION

Due to type erasure, casts involving generic types are unchecked. The compiler warns about this but allows it. The actual cast check only verifies the raw type.

Test
Q.14 Medium Generics
Given the declaration: List listOfLists;
Which assignment is valid?
A listOfLists = new ArrayList();
B listOfLists = new ArrayList
C listOfLists = new ArrayList();
D listOfLists = new ArrayList();
Correct Answer:  D. listOfLists = new ArrayList();
EXPLANATION

Nested generics must match exactly or use appropriate wildcards. Only option D provides exact type match. Option B would work with wildcards, but that's not listed correctly here.

Test
Q.15 Medium Generics
What is the correct way to declare a generic method that accepts a list and returns the maximum element?
A public T getMax(List list) { ... }
B public static T getMax(List
C public
D public T getMax(List list) { ... }
Correct Answer:  D. public T getMax(List list) { ... }
EXPLANATION

Option D correctly ensures T is comparable with itself using F-bounded polymorphism. Option C has inverted logic with '? super'. Option B is unnecessarily complex.

Test
Q.16 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'.

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

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

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

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

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