Java interviews and campus tests lean heavily on object oriented behaviour rather than API recall. This set covers inheritance and polymorphism, interfaces and abstract classes, the collections framework, exception handling, multithreading, string handling, and JVM basics such as garbage collection. Output based questions are included because they expose the gaps that theory questions hide.
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.42Medium
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.43Medium
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.44Medium
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.45Medium
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.46Easy
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.47Medium
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.
Q.48Medium
What happens when you use raw type List instead of List<String>?
Answer: D
Using raw types bypasses generic type checking, losing type safety. Elements are treated as Object at runtime, potentially causing ClassCastException when casting.
Q.49Hard
Which generic declaration allows a method to accept List of any type?
Answer: D
All three approaches allow processing lists of any type, though they differ: raw type (unsafe), unbounded wildcard (read-only), and type parameter (flexible).
Q.50Hard
What is the difference between <T extends Comparable<T>> and <T extends Comparable>?
Answer: B
<T extends Comparable<T>> is a recursive bound ensuring T implements Comparable of its own type. <T extends Comparable> uses raw type, losing type information.
Q.51Hard
Can you create an instance of generic type parameter directly like: T obj = new T();?
Answer: B
Due to type erasure, T is replaced with Object at runtime, and you cannot instantiate Object with new T(). You need reflection or pass Class<T> as parameter.
Q.52Hard
What is the PECS principle in generics?
Answer: A
PECS (Producer Extends, Consumer Super) is a mnemonic for wildcard bounds: use '? extends' when reading (producing), use '? super' when writing (consuming).
Q.53Hard
Which statement about generic constructors is TRUE?
Answer: D
Generic constructors can exist in non-generic classes. Constructor's type parameters are separate from and independent of the class's type parameters.
Q.54Medium
What is the output?
List<String> strings = new ArrayList<String>(); List list = strings; list.add(123); String str = strings.get(0); System.out.println(str);
Answer: C
Adding Integer 123 to raw type reference bypasses generics. When retrieving as String, a ClassCastException occurs at runtime.
Q.55Medium
How would you define a generic method that works with a pair of objects and returns the first one?
Answer: D
Both A and C define valid generic methods with multiple type parameters. C is more explicit with 'public' modifier, but both are functionally equivalent.
Q.56Easy
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.57Easy
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.58Easy
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.59Medium
Consider the following code snippet: public <T> T processData(List<? extends T> list) { return list.get(0); }
What is the relationship between the wildcard and type parameter T?
Answer: B
The wildcard '? extends T' means the list can contain any type that is a subtype of T. This allows reading from the list safely but restricts writing.
Q.60Medium
Which of the following will cause a compile-time error?
Answer: B
Java does not allow covariance with generics. List<Integer> cannot be assigned to List<Number> because you could add a Double to the latter, breaking type safety.