Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 201–210 of 958
Topics in Java Programming
Q.201 Easy Generics
What is the result of type erasure in Java generics?
A Generic type information is preserved at runtime
B All generic type information is removed at compile time, replaced with raw types or bounds
C Generic types are converted to Object only
D Type erasure only applies to wildcards
Correct Answer:  B. All generic type information is removed at compile time, replaced with raw types or bounds
EXPLANATION

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.

Take Test
Q.202 Easy Generics
Which of the following is a valid generic class declaration in Java?
A public class Box { }
B public class Box { }
C public class Box { }
D public class Box { }
Correct Answer:  A. public class Box { }
EXPLANATION

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.

Take Test
Q.203 Easy Generics
What is the advantage of using generics over raw types in terms of 2024-25 Java standards?
A Compile-time type checking prevents ClassCastException
B Enables code reuse without sacrificing type safety
C Improves API clarity and documentation
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

Generics provide compile-time safety, code reusability through parameterization, and clearer API contracts. These are essential for modern Java development standards.

Take Test
Q.204 Medium Generics
How would you define a generic method that works with a pair of objects and returns the first one?
A T getFirst(T first, U second) { return first; }
B T getFirst(T first, T second) { return first; }
C public T getFirst(T first, U second) { return first; }
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

Both A and C define valid generic methods with multiple type parameters. C is more explicit with 'public' modifier, but both are functionally equivalent.

Take Test
Q.205 Medium Generics
What is the output?

List strings = new ArrayList();
List list = strings;
list.add(123);
String str = strings.get(0);
System.out.println(str);
A 123
B Compilation error
C Runtime ClassCastException
D null
Correct Answer:  C. Runtime ClassCastException
EXPLANATION

Adding Integer 123 to raw type reference bypasses generics. When retrieving as String, a ClassCastException occurs at runtime.

Take Test
Q.206 Hard Generics
Which statement about generic constructors is TRUE?
A Generic constructors must be in generic classes only
B Non-generic classes can have generic constructors
C Constructor type parameters are independent of class type parameters
D Both B and C
Correct Answer:  D. Both B and C
EXPLANATION

Generic constructors can exist in non-generic classes. Constructor's type parameters are separate from and independent of the class's type parameters.

Take Test
Q.207 Hard Generics
What is the PECS principle in generics?
A Producer Extends, Consumer Super
B Public Extends, Consumer Sets
C Parameter Encapsulation and Class Structure
D Polymorphic Extension and Class Signatures
Correct Answer:  A. Producer Extends, Consumer Super
EXPLANATION

PECS (Producer Extends, Consumer Super) is a mnemonic for wildcard bounds: use '? extends' when reading (producing), use '? super' when writing (consuming).

Take Test
Q.208 Hard Generics
Can you create an instance of generic type parameter directly like: T obj = new T();?
A Yes, always possible
B No, because type information is erased at runtime
C Yes, only if T extends Serializable
D Only with reflection
Correct Answer:  B. No, because type information is erased at runtime
EXPLANATION

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.

Take Test
Q.209 Hard Generics
What is the difference between and ?
A No difference; both are equivalent
B First ensures T is comparable with itself; second is a raw type
C First is deprecated in Java 8+
D Second provides better type safety
Correct Answer:  B. First ensures T is comparable with itself; second is a raw type
EXPLANATION

<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.

Take Test
Q.210 Hard Generics
Which generic declaration allows a method to accept List of any type?
A public void process(List rawList) { }
B public void process(List list) { }
C public void process(List list) { }
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

All three approaches allow processing lists of any type, though they differ: raw type (unsafe), unbounded wildcard (read-only), and type parameter (flexible).

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips