Java Programming — Generics
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 100 questions in Generics
Q.21 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>.

Take Test
Q.22 Easy Generics
Which declaration is valid in Java Generics?
A List list = new ArrayList();
B List list = new ArrayList();
C List list = new ArrayList();
D List
Correct Answer:  B. List list = new ArrayList();
EXPLANATION

Generics only work with reference types, not primitive types. Integer is the wrapper class for int, making option B valid.

Take Test
Q.23 Easy Generics
Which of the following correctly declares a generic method that returns the first element of any collection?
A public T getFirst(Collection collection)
B public T getFirst(Collection collection)
C public T getFirst(Collection collection)
D public Collection getFirst(T collection)
Correct Answer:  B. public T getFirst(Collection collection)
EXPLANATION

Generic methods require type parameter declaration before return type. The collection parameter must be of type Collection<T> to extract element of type T.

Take Test
Q.24 Easy Generics
What will be the output of the following code?
java
List list = new ArrayList();
list.add("Java");
Object obj = list;
System.out.println(list.getClass() == obj.getClass());
A true
B false
C Compilation error
D Runtime exception
Correct Answer:  A. true
EXPLANATION

Generic type information is erased at runtime. Both list and obj refer to the same ArrayList class, so getClass() returns the same class object.

Take Test
Q.25 Hard Generics
How would you correctly use generics in a recursive type bound scenario?
A class Node { Node next; }
B class Node { Node next; }
C class Node extends T { }
D class Node { }
Correct Answer:  A. class Node { Node next; }
EXPLANATION

Option A demonstrates recursive type bound (also called F-bounded polymorphism), which allows a type to reference itself in its bound. This is commonly used in builder patterns and comparators.

Take Test
Advertisement
Q.26 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.

Take Test
Q.27 Easy Generics
What is the relationship between generics and arrays in Java?
A Generic arrays can be created freely like List[]
B Generic arrays are not supported; you cannot create arrays of parameterized types
C Generic arrays work only with unbounded wildcards
D Arrays of generics work only for primitive types
Correct Answer:  B. Generic arrays are not supported; you cannot create arrays of parameterized types
EXPLANATION

Java does not allow creation of arrays with parameterized types due to type erasure. List<String>[] would be unsafe. You must use List<String>[] or List[] instead.

Take Test
Q.28 Hard Generics
Which of the following represents valid bounded wildcard usage for a method that processes collections?
A public void process(Collection
B public void process(Collection
C public void process(Collection col) { col.add(5); }
D public void process(Collection col) { col.add(5); }
Correct Answer:  B. public void process(Collection
EXPLANATION

Only option B allows adding elements safely. '? super Integer' means the collection can hold Integer or any supertype. Option A prevents adding, Option C prevents adding, Option D restricts to exactly Number.

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

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

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