Java Programming — Generics
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 81–90 of 100 questions in Generics
Q.81 Easy Generics
In Java generics, what does the wildcard '?' represent?
A An unknown type that can be any class
B A mandatory type parameter
C A primitive data type only
D A deprecated feature in Java 8+
Correct Answer:  A. An unknown type that can be any class
EXPLANATION

The wildcard '?' in generics represents an unknown type, allowing flexibility when the exact type is not known or not important.

Take Test
Q.82 Medium Generics
Which of these declarations would NOT cause a compilation warning or error?
A List list = new ArrayList();
B List list = new ArrayList();
C List list = new ArrayList();
D List list = new ArrayList();
Correct Answer:  A. List list = new ArrayList();
EXPLANATION

Option A uses proper diamond syntax with matching type parameters on both sides. Options B, C produce unchecked warnings, and Option D is invalid due to invariance.

Take Test
Q.83 Hard Generics
What will be the result of this code?
List list = new ArrayList();
list.add("Hello");
List raw = list; // Unchecked assignment
raw.add(123); // Adding Integer to raw type
String s = list.get(1);
A ClassCastException when accessing list.get(1)
B Code executes without error
C Compilation error
D StringIndexOutOfBoundsException
Correct Answer:  A. ClassCastException when accessing list.get(1)
EXPLANATION

Raw type assignment bypasses generics. Integer 123 is added to the list. When casting to String at get(1), ClassCastException occurs.

Take Test
Q.84 Hard Generics
Which statement is true about generic inheritance?
A ArrayList is NOT a subtype of List due to invariance
B ArrayList IS a subtype of List
C ArrayList is a subtype of ArrayList
D Generic types support covariance by default
Correct Answer:  B. ArrayList IS a subtype of List
EXPLANATION

ArrayList<Integer> is a subtype of List<Integer> because ArrayList is a subclass of List with the same type parameter.

Take Test
Q.85 Easy Generics
What is the output of this generic code?
class Pair {
public void display(K key, V value) {
System.out.println(key + ": " + value);
}
}
Pair p = new Pair();
p.display("Age", 25);
A Age: 25
B Compilation error - type mismatch
C Age: 25.0
D Runtime exception
Correct Answer:  A. Age: 25
EXPLANATION

The generic class Pair with two type parameters K and V correctly accepts String and Integer. The display method prints the key-value pair.

Take Test
Q.86 Hard Generics
Consider this code:
public T getMax(T a, T b) {
return a.compareTo(b) > 0 ? a : b;
}
What is the benefit of this recursive bound ?
A Ensures type safety by guaranteeing T implements Comparable with itself
B Allows T to be compared with any type
C Improves runtime performance
D Eliminates the need for type erasure
Correct Answer:  A. Ensures type safety by guaranteeing T implements Comparable with itself
EXPLANATION

Recursive bound <T extends Comparable<T>> ensures that T implements Comparable interface specifically for comparing with its own type, providing type safety.

Take Test
Q.87 Hard Generics
What will happen with this code?
List list = new ArrayList();
list.add(123); // Adding Integer
String s = (String) list.get(0);
A ClassCastException at runtime
B Compilation error
C Code executes successfully
D Unchecked warning at compile time only
Correct Answer:  A. ClassCastException at runtime
EXPLANATION

Raw type List accepts any object. At runtime, the Integer 123 cannot be cast to String, causing ClassCastException.

Take Test
Q.88 Hard Generics
Which of these correctly demonstrates the Producer Extends Consumer Super (PECS) principle?
A public void process(List
B public void process(List
C public void process(List source, List dest) { }
D public void process(List source, List dest) { }
Correct Answer:  A. public void process(List
EXPLANATION

PECS principle: use 'extends' when reading from a collection (source), use 'super' when writing to it (dest). Option A reads from source and writes to dest correctly.

Take Test
Q.89 Medium Generics
What will be the output?
List list = new ArrayList();
list.add(10);
Object obj = list.get(0);
System.out.println(obj instanceof Integer);
A true
B false
C Compilation error
D Runtime exception
Correct Answer:  A. true
EXPLANATION

The Integer value is stored in the list. At runtime, type erasure converts it to Object, but the actual stored value remains an Integer instance.

Take Test
Q.90 Medium Generics
Consider this generic interface:
interface Comparable { int compareTo(T o); }
Which class definition correctly implements this?
A class MyClass implements Comparable { public int compareTo(MyClass o) { return 0; } }
B class MyClass implements Comparable { public int compareTo(Object o) { return 0; } }
C class MyClass implements Comparable { public int compareTo(Object o) { return 0; } }
D class MyClass implements Comparable { public int compareTo(T o) { return 0; } }
Correct Answer:  A. class MyClass implements Comparable { public int compareTo(MyClass o) { return 0; } }
EXPLANATION

Option A correctly implements the generic interface by specifying the concrete type parameter as MyClass in the implements clause.

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