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.722Medium
Which generic wildcard usage is INCORRECT?
Answer: D
Nested wildcards like '? extends ? super T' are not allowed. Wildcards cannot be combined or nested.
Q.723Medium
What is the relationship between raw types and generics in Java?
Answer: A
Raw types (like 'List' without type parameters) are legacy and should be avoided as they bypass compile-time type safety.
Q.724Medium
Consider the method: public static <T> T getFirst(List<T> list) { return list.get(0); }
How would you call this method for a List<String>?
Answer: D
Type inference in Java allows the compiler to deduce T from the argument type, so both explicit and implicit type specification work.
Q.725Hard
What will happen when you try to create an array of generic types like 'new ArrayList<String>[10]'?
Answer: A
Arrays of generic types are not allowed in Java due to type erasure and heap pollution prevention.
Advertisement
Q.726Easy
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.727Medium
What is the significance of the '&' operator in generic bounds like '<T extends A & B>'?
Answer: A
Multiple bounds (using &) mean T must satisfy all constraints. This is useful when a type needs to implement multiple interfaces.
Q.728Hard
Consider: Map<String, ? extends List<?>> map = new HashMap<>();
What can you safely retrieve from this map?
Answer: A
The value type is '? extends List<?>', so you get a List with unknown element type. The exact subtype is unknown at compile-time.
Q.729Medium
Which statement about generic type parameters is TRUE?
Answer: C
While convention uses single uppercase letters, type parameters can be any valid identifier. Statements B and D are also incorrect.
Q.730Hard
What is the key difference between '? extends T' and '? super T' in practical usage?
Answer: A
Upper bounds (? extends T) are safe for reading because the compiler knows it's at least T. Lower bounds (? super T) are safe for writing because you can pass T or any superclass.
Q.731Easy
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.732Easy
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.733Medium
Consider the declaration: List<? super Integer> list. What does this wildcard represent?
Answer: B
'? super Integer' is a lower bounded wildcard that accepts Integer and all its superclasses like Number, Object. This allows write operations with Integer values.
Q.734Easy
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.735Medium
Which statement correctly defines a generic class with multiple type parameters and bounds?
Answer: C
Multiple type parameters with bounds must be comma-separated and each can have its own upper bound. Option C shows correct syntax with recursive bound on T and bound on K.
Q.736Medium
Consider: public static <T> T findMax(T a, T b) { return a; }. What is the issue with calling findMax(5, 3.5)?
Answer: A
The type parameter T cannot simultaneously be int and double. The compiler cannot infer a single type T that satisfies both arguments, resulting in a compilation error.
Q.737Medium
What does List<?> represent in Java generics?
Answer: D
List<?> is an unbounded wildcard that represents a list of unknown type. You can read from it (get Object), but cannot write to it (except null). Both A, B, and C are correct interpretations.
Q.738Medium
Which of the following will NOT compile?
Answer: D
Option D fails because List<Object> cannot reference ArrayList<String>. Generics are invariant. Options A, B, C are valid (covariance with extends, contravariance with super).
Q.739Easy
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.740Medium
Consider a generic interface: interface Comparable<T> { int compareTo(T obj); }. How should a class implement this for String comparison?
Answer: D
Option B directly specifies String, while Option C makes the class generic. Both are valid approaches to implement a generic interface.