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.5Easy
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);
Advertisement
Q.6Easy
In Java generics, what does the wildcard '?' represent?
Q.7Easy
What is the primary advantage of using generics in Java?
Q.8Easy
Which of the following will compile successfully?
Q.9Easy
What is the output of this code snippet?
ArrayList<String> list = new ArrayList<>();
list.add("Java");
Object obj = list.get(0);
String str = (String) obj;
System.out.println(str.length());
Q.10Easy
In the interface declaration 'interface Pair<K, V>', how many type parameters does it have?
Q.11Easy
Which of the following is a valid generic method declaration in Java?
Q.12Easy
What is the primary purpose of bounded type parameters in generics?
Q.13Easy
What will be the result of executing: List<Integer> list = new ArrayList<String>();
Q.14Easy
What is the output of the following code?
List<Integer> list = new ArrayList<Integer>();
list.add(10);
Object obj = list.get(0);
System.out.println(obj.getClass().getName());
Q.15Easy
What is the advantage of using generics over raw types in terms of 2024-25 Java standards?
Q.16Easy
Which of the following is a valid generic class declaration in Java?
Q.17Easy
What is the result of type erasure in Java generics?
Q.18Easy
What is the compiler-inferred type parameter in this call?
List<String> list = Arrays.asList("a", "b", "c");
Q.19Easy
What does the wildcard in List<?> represent?
Q.20Easy
What is the relationship between generics and arrays in Java?