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

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

Take Test
Q.55 Easy Generics
What is the output of the following code?

List list = new ArrayList();
list.add(10);
Object obj = list.get(0);
System.out.println(obj.getClass().getName());
A java.lang.Integer
B java.lang.Object
C java.lang.Number
D Compilation error
Correct Answer:  A. java.lang.Integer
EXPLANATION

Although obj is declared as Object, it holds an Integer instance. getClass() returns the actual runtime type, which is Integer.

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

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

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

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

Take Test
Q.60 Easy Generics
What will be the result of executing: List list = new ArrayList();
A Compilation error
B Runtime error
C Compiles and runs successfully
D Warning at compile time only
Correct Answer:  A. Compilation error
EXPLANATION

This causes a compilation error due to type mismatch. List<Integer> cannot hold ArrayList<String>. Generics do not support implicit type conversion.

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