Generics enable type safety at compile time and eliminate the need for explicit type casting, reducing runtime errors.
Q.2Easy
Which keyword is used to declare a generic class in Java?
Answer: A
<T> is the standard syntax for declaring a generic type parameter in Java classes.
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());
Answer: A
"Java" has 4 characters. The generic type ensures the element is a String, so .length() method is available without casting.
Q.4Easy
Which of the following is a valid generic method declaration?
Answer: A
The correct syntax for a generic method is: access_modifier <T> returnType methodName(T parameter). The type parameter <T> must come before the return type.
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);
Answer: A
The generic class Pair with two type parameters K and V correctly accepts String and Integer. The display method prints the key-value pair.
Advertisement
Q.6Easy
In Java generics, what does the wildcard '?' represent?
Answer: A
The wildcard '?' in generics represents an unknown type, allowing flexibility when the exact type is not known or not important.
Q.7Easy
What is the primary advantage of using generics in Java?
Answer: A
Generics provide compile-time type checking, preventing ClassCastException and eliminating the need for explicit casting at runtime.
Q.8Easy
Which of the following will compile successfully?
Answer: B
Generics only work with reference types. 'int' is a primitive type, so 'Integer' wrapper class must be used instead.
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());
Answer: A
The code compiles and runs correctly. 'Java' has 4 characters. The explicit cast is necessary because get() returns Object type.
Q.10Easy
In the interface declaration 'interface Pair<K, V>', how many type parameters does it have?
Answer: B
The interface has two type parameters: K and V, making it a generic interface that can be implemented with different type combinations.
Q.11Easy
Which of the following is a valid generic method declaration in Java?
Answer: A
Type parameters in Java must be declared before the return type using angle brackets. Option A has correct syntax: <T> comes before return type void.
Q.12Easy
What is the primary purpose of bounded type parameters in generics?
Answer: A
Bounded type parameters like <T extends Number> restrict the types that can be passed as type arguments, ensuring type safety and enabling access to specific methods.
Q.13Easy
What will be the result of executing: List<Integer> list = new ArrayList<String>();
Answer: A
This causes a compilation error due to type mismatch. List<Integer> cannot hold ArrayList<String>. Generics do not support implicit type conversion.
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());
Answer: A
Although obj is declared as Object, it holds an Integer instance. getClass() returns the actual runtime type, which is Integer.
Q.15Easy
What is the advantage of using generics over raw types in terms of 2024-25 Java standards?
Answer: D
Generics provide compile-time safety, code reusability through parameterization, and clearer API contracts. These are essential for modern Java development standards.
Q.16Easy
Which of the following is a valid generic class declaration in Java?
Answer: A
Valid generic class declarations allow multiple type parameters with upper bounds. Option A is syntactically correct. Option C uses invalid 'super' keyword in class declaration. Option D has malformed syntax.
Q.17Easy
What is the result of type erasure in Java generics?
Answer: B
Type erasure removes all generic type parameters during compilation. For unbounded types, they're replaced with Object; for bounded types, they're replaced with the upper bound.
Q.18Easy
What is the compiler-inferred type parameter in this call?
List<String> list = Arrays.asList("a", "b", "c");
Answer: B
The compiler infers the most specific common type of the arguments. Since all arguments are String literals, T is inferred as String.
Q.19Easy
What does the wildcard in List<?> represent?
Answer: B
List<?> represents unknown type with read-only capabilities. Elements can only be read as Object. You cannot add elements (except null) due to type safety.
Q.20Easy
What is the relationship between generics and arrays in Java?
Answer: B
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.