Home Subjects Java Programming Generics

Java Programming
Generics

Java OOP, collections, multithreading

49 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 41–49 of 49
Topics in Java Programming
Q.41 Medium Generics
Which of these declarations would NOT cause a compilation warning or error?
A List list = new ArrayList();
B List list = new ArrayList();
C List list = new ArrayList();
D List list = new ArrayList();
Correct Answer:  A. List list = new ArrayList();
EXPLANATION

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.

Test
Q.42 Medium Generics
What will be the output?
List list = new ArrayList();
list.add(10);
Object obj = list.get(0);
System.out.println(obj instanceof Integer);
A true
B false
C Compilation error
D Runtime exception
Correct Answer:  A. true
EXPLANATION

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.

Test
Q.43 Medium Generics
Consider this generic interface:
interface Comparable { int compareTo(T o); }
Which class definition correctly implements this?
A class MyClass implements Comparable { public int compareTo(MyClass o) { return 0; } }
B class MyClass implements Comparable { public int compareTo(Object o) { return 0; } }
C class MyClass implements Comparable { public int compareTo(Object o) { return 0; } }
D class MyClass implements Comparable { public int compareTo(T o) { return 0; } }
Correct Answer:  A. class MyClass implements Comparable { public int compareTo(MyClass o) { return 0; } }
EXPLANATION

Option A correctly implements the generic interface by specifying the concrete type parameter as MyClass in the implements clause.

Test
Q.44 Medium Generics
What is the difference between List and List?
A List is read-only while List allows adding any object type
B List is read-only while List allows adding any type
C Both are equivalent
D List uses type erasure while List does not
Correct Answer:  A. List is read-only while List allows adding any object type
EXPLANATION

List<?> accepts any type but is read-only (can't add). List<Object> explicitly accepts Object type and allows adding objects.

Test
Q.45 Medium Generics
Which of the following represents a bounded type parameter?
A class Box { }
B class Box { }
C class Box
D class Box { }
Correct Answer:  A. class Box { }
EXPLANATION

Bounded type parameters restrict the types that can be used as arguments. T extends Number ensures T is Number or its subclass.

Test
Q.46 Medium Generics
Consider the code:
List
A No, compilation error - cannot add to ? extends
B Yes, it compiles successfully
C Only if Integer is used
D Only at runtime
Correct Answer:  A. No, compilation error - cannot add to ? extends
EXPLANATION

Upper bounded wildcards (? extends Number) are read-only. You cannot add elements because the compiler doesn't know the exact subtype.

Test
Q.47 Medium Generics
What will happen when you try to create an array of generics?
List[] array = new List[10];
A Compilation error
B Runtime exception
C Code executes successfully
D Creates an unchecked warning only
Correct Answer:  A. Compilation error
EXPLANATION

You cannot create arrays of generic types because of type erasure. The compiler prevents this with a compilation error.

Test
Q.48 Medium Generics
Which statement about wildcard generics (?) is correct?
A ? represents an unknown type and can be used with upper bounds (? extends T) or lower bounds (? super T)
B ? can only be used with extends keyword
C ? is used to create generic classes
D ? eliminates the need for type parameters
Correct Answer:  A. ? represents an unknown type and can be used with upper bounds (? extends T) or lower bounds (? super T)
EXPLANATION

Wildcards (?) represent an unknown type. Upper bound (? extends T) accepts T or its subtypes. Lower bound (? super T) accepts T or its supertypes.

Test
Q.49 Medium Generics
What is type erasure in Java Generics?
A The process by which generic type information is removed at runtime
B Automatic deletion of generic collections
C Compiler optimization of generic code
D Runtime type checking for generics
Correct Answer:  A. The process by which generic type information is removed at runtime
EXPLANATION

Type erasure is a mechanism where the Java compiler removes all generic type information during compilation, converting generic code to its raw type equivalent.

Test
IGET
IGET AI
Online · Exam prep assistant
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