In JDBC 2024-25, which exception is thrown when attempting to use a closed Connection object?
Answer: A
A SQLException (specifically a SQLNonTransientConnectionException as a subclass) is thrown when operations are attempted on a closed Connection. SQLException is the general exception for database access errors.
Q.342Medium
What is type erasure in Java Generics?
Answer: A
Type erasure is a mechanism where the Java compiler removes all generic type information during compilation, converting generic code to its raw type equivalent.
Q.343Medium
Which statement about wildcard generics (?) is correct?
Answer: A
Wildcards (?) represent an unknown type. Upper bound (? extends T) accepts T or its subtypes. Lower bound (? super T) accepts T or its supertypes.
Q.344Medium
What will happen when you try to create an array of generics?
List<String>[] array = new List<String>[10];
Answer: A
You cannot create arrays of generic types because of type erasure. The compiler prevents this with a compilation error.
Q.345Medium
Consider the code:
List<? extends Number> list = new ArrayList<>();
list.add(5); // Will this compile?
Answer: A
Upper bounded wildcards (? extends Number) are read-only. You cannot add elements because the compiler doesn't know the exact subtype.
Advertisement
Q.346Medium
Which of the following represents a bounded type parameter?
Answer: A
Bounded type parameters restrict the types that can be used as arguments. T extends Number ensures T is Number or its subclass.
Q.347Medium
What is the difference between List<?> and List<Object>?
Answer: A
List<?> accepts any type but is read-only (can't add). List<Object> explicitly accepts Object type and allows adding objects.
Q.348Medium
Consider this generic interface:
interface Comparable<T> { int compareTo(T o); }
Which class definition correctly implements this?
Answer: A
Option A correctly implements the generic interface by specifying the concrete type parameter as MyClass in the implements clause.
Q.349Medium
What will be the output?
List<Integer> list = new ArrayList<>();
list.add(10);
Object obj = list.get(0);
System.out.println(obj instanceof Integer);
Answer: A
The Integer value is stored in the list. At runtime, type erasure converts it to Object, but the actual stored value remains an Integer instance.
Q.350Medium
Which of these declarations would NOT cause a compilation warning or error?
Answer: A
Option A uses proper diamond syntax with matching type parameters on both sides. Options B, C produce unchecked warnings, and Option D is invalid due to invariance.
Q.351Medium
In the declaration 'List<? extends Number> list', what types can be added to this list at runtime?
Answer: C
Upper-bounded wildcards (? extends T) are used for reading data. Adding elements is unsafe because the compiler doesn't know the exact type.
Q.352Medium
Which statement correctly uses the lower-bounded wildcard in generics?
Answer: A
Lower-bounded wildcards (? super T) allow superclasses of T. This assignment is valid because ArrayList<Number> fits '? super Integer'.
Q.353Medium
Which generic declaration would allow storing both String and Integer in the same collection?
Answer: D
All three allow storing multiple types because they use unbounded or Object-level wildcards, though List<?> is most restrictive for additions.
Q.354Medium
What does the PECS principle stand for in generics context?
Answer: A
PECS guides when to use upper bounds (? extends) for producers/readers and lower bounds (? super) for consumers/writers.
Q.355Medium
In a generic class 'class Box<T extends Comparable<T>>', what constraint is placed on T?
Answer: A
The bound 'T extends Comparable<T>' ensures T is comparable to itself, enabling type-safe comparison operations.
Q.356Medium
Which generic wildcard usage is INCORRECT?
Answer: D
Nested wildcards like '? extends ? super T' are not allowed. Wildcards cannot be combined or nested.
Q.357Medium
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.358Medium
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.359Medium
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.360Medium
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.