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.
Q.202Easy
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.203Easy
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.204Easy
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.205Easy
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.
Advertisement
Q.206Easy
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.207Easy
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.208Easy
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.209Easy
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.210Easy
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.211Easy
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.212Easy
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.213Easy
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.214Easy
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.215Easy
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.216Easy
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.
Q.217Easy
What will be the output of the following code?
java
List<String> list = new ArrayList<String>();
list.add("Java");
Object obj = list;
System.out.println(list.getClass() == obj.getClass());
Answer: A
Generic type information is erased at runtime. Both list and obj refer to the same ArrayList class, so getClass() returns the same class object.
Q.218Easy
Which of the following correctly declares a generic method that returns the first element of any collection?
Answer: B
Generic methods require type parameter declaration before return type. The collection parameter must be of type Collection<T> to extract element of type T.
Q.219Easy
Which declaration is valid in Java Generics?
Answer: B
Generics only work with reference types, not primitive types. Integer is the wrapper class for int, making option B valid.
Q.220Easy
In a Spring Boot application, you need to create a generic repository interface that works with any entity type. Which declaration correctly implements this pattern?
Answer: A
Option A uses single type parameter T for entity type, which is the standard pattern for generic repositories. Option C is redundant (all classes extend Object), and Option D adds unnecessary complexity.