Java Programming — Generics
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 91–100 of 100 questions in Generics
Q.91 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.

Take Test
Q.92 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.

Take Test
Q.93 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.

Take Test
Q.94 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.

Take Test
Q.95 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.

Take Test
Advertisement
Q.96 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.

Take Test
Q.97 Easy Generics
Which of the following is a valid generic method declaration?
A public T getValue(T value) { return value; }
B public T getValue(T value) { return value; }
C public getValue(T value) { return value; }
D public getValue(T value) { return value; }
Correct Answer:  A. public T getValue(T value) { return value; }
EXPLANATION

The correct syntax for a generic method is: access_modifier <T> returnType methodName(T parameter). The type parameter <T> must come before the return type.

Take Test
Q.98 Easy Generics
What will be the output of the following code?
List list = new ArrayList();
list.add("Java");
System.out.println(list.get(0).length());
A 4
B 5
C Compilation error
D Runtime exception
Correct Answer:  A. 4
EXPLANATION

"Java" has 4 characters. The generic type ensures the element is a String, so .length() method is available without casting.

Take Test
Q.99 Easy Generics
Which keyword is used to declare a generic class in Java?
A
B generic
C template
D type
Correct Answer:  A.
EXPLANATION

<T> is the standard syntax for declaring a generic type parameter in Java classes.

Take Test
Q.100 Easy Generics
What is the primary purpose of Generics in Java?
A To provide type safety and eliminate type casting
B To increase memory allocation
C To reduce code compilation time
D To automatically manage garbage collection
Correct Answer:  A. To provide type safety and eliminate type casting
EXPLANATION

Generics enable type safety at compile time and eliminate the need for explicit type casting, reducing runtime errors.

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