Which keyword is used to declare a generic class in Java?
Q.3Easy
What will be the output of the following code?
List<String> list = new ArrayList<>();
list.add("Java");
System.out.println(list.get(0).length());
Q.4Easy
Which of the following is a valid generic method declaration?
Q.5Medium
What is type erasure in Java Generics?
Advertisement
Q.6Medium
Which statement about wildcard generics (?) is correct?
Q.7Medium
What will happen when you try to create an array of generics?
List<String>[] array = new List<String>[10];
Q.8Medium
Consider the code:
List<? extends Number> list = new ArrayList<>();
list.add(5); // Will this compile?
Q.9Medium
Which of the following represents a bounded type parameter?
Q.10Medium
What is the difference between List<?> and List<Object>?
Q.11Medium
Consider this generic interface:
interface Comparable<T> { int compareTo(T o); }
Which class definition correctly implements this?
Q.12Medium
What will be the output?
List<Integer> list = new ArrayList<>();
list.add(10);
Object obj = list.get(0);
System.out.println(obj instanceof Integer);
Q.13Hard
Which of these correctly demonstrates the Producer Extends Consumer Super (PECS) principle?
Q.14Hard
What will happen with this code?
List list = new ArrayList<String>();
list.add(123); // Adding Integer
String s = (String) list.get(0);
Q.15Hard
Consider this code:
public <T extends Comparable<T>> T getMax(T a, T b) {
return a.compareTo(b) > 0 ? a : b;
}
What is the benefit of this recursive bound <T extends Comparable<T>>?
Q.16Easy
What is the output of this generic code?
class Pair<K, V> {
public void display(K key, V value) {
System.out.println(key + ": " + value);
}
}
Pair<String, Integer> p = new Pair<>();
p.display("Age", 25);
Q.17Hard
Which statement is true about generic inheritance?
Q.18Hard
What will be the result of this code?
List<String> list = new ArrayList<>();
list.add("Hello");
List raw = list; // Unchecked assignment
raw.add(123); // Adding Integer to raw type
String s = list.get(1);
Q.19Medium
Which of these declarations would NOT cause a compilation warning or error?
Q.20Easy
In Java generics, what does the wildcard '?' represent?