Java Programming — Generics
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 61–70 of 100 questions in Generics
Q.61 Medium Generics
Consider the declaration: List
A List of Integer and its subclasses
B List of Integer and its superclasses
C List of any type
D List of Number and Integer only
Correct Answer:  B. List of Integer and its superclasses
EXPLANATION

'? super Integer' is a lower bounded wildcard that accepts Integer and all its superclasses like Number, Object. This allows write operations with Integer values.

Take Test
Q.62 Easy Generics
What is the primary purpose of bounded type parameters in generics?
A To restrict type arguments to specific types or their subtypes
B To improve code readability only
C To increase runtime performance
D To create multiple inheritance in Java
Correct Answer:  A. To restrict type arguments to specific types or their subtypes
EXPLANATION

Bounded type parameters like <T extends Number> restrict the types that can be passed as type arguments, ensuring type safety and enabling access to specific methods.

Take Test
Q.63 Easy Generics
Which of the following is a valid generic method declaration in Java?
A public static void display(T data) { }
B public static void display(T data) { }
C public static void display(T data) { }
D public static void display(T data) { }
Correct Answer:  A. public static void display(T data) { }
EXPLANATION

Type parameters in Java must be declared before the return type using angle brackets. Option A has correct syntax: <T> comes before return type void.

Take Test
Q.64 Hard Generics
What is the key difference between '? extends T' and '? super T' in practical usage?
A Extends is for reading data safely, Super is for writing data safely
B Extends is for writing, Super is for reading
C They have no practical difference
D Extends is for classes, Super is for interfaces
Correct Answer:  A. Extends is for reading data safely, Super is for writing data safely
EXPLANATION

Upper bounds (? extends T) are safe for reading because the compiler knows it's at least T. Lower bounds (? super T) are safe for writing because you can pass T or any superclass.

Take Test
Q.65 Medium Generics
Which statement about generic type parameters is TRUE?
A Type parameters must be uppercase single letters (T, U, V, etc.)
B Type parameters can only be used in class declarations, not in method declarations
C Type parameters can be any valid Java identifier
D Type parameters must match the actual type used at instantiation
Correct Answer:  C. Type parameters can be any valid Java identifier
EXPLANATION

While convention uses single uppercase letters, type parameters can be any valid identifier. Statements B and D are also incorrect.

Take Test
Q.66 Hard Generics
Consider: Map
A A List of unknown element type
B A List
C Any List type
D Nothing can be safely retrieved
Correct Answer:  A. A List of unknown element type
EXPLANATION

The value type is '? extends List<?>', so you get a List with unknown element type. The exact subtype is unknown at compile-time.

Take Test
Q.67 Medium Generics
What is the significance of the '&' operator in generic bounds like ''?
A T must extend both A and B (multiple bounds)
B T must extend A or B
C It's a bitwise AND operation
D T extends A with B as an alternative
Correct Answer:  A. T must extend both A and B (multiple bounds)
EXPLANATION

Multiple bounds (using &) mean T must satisfy all constraints. This is useful when a type needs to implement multiple interfaces.

Take Test
Q.68 Easy Generics
In the interface declaration 'interface Pair', how many type parameters does it have?
A One
B Two
C Variable depending on implementation
D None, they are method parameters
Correct Answer:  B. Two
EXPLANATION

The interface has two type parameters: K and V, making it a generic interface that can be implemented with different type combinations.

Take Test
Q.69 Hard Generics
What will happen when you try to create an array of generic types like 'new ArrayList[10]'?
A Compilation error because arrays of generic types are not allowed
B Runtime error after successful compilation
C It will compile and run successfully
D Warning but no compilation error
Correct Answer:  A. Compilation error because arrays of generic types are not allowed
EXPLANATION

Arrays of generic types are not allowed in Java due to type erasure and heap pollution prevention.

Take Test
Q.70 Medium Generics
Consider the method: public static T getFirst(List list) { return list.get(0); }

How would you call this method for a List?
A String result = Main.getFirst(stringList);
B String result = getFirst(stringList);
C String result = Main.getFirst(stringList);
D Both A and B are correct
Correct Answer:  D. Both A and B are correct
EXPLANATION

Type inference in Java allows the compiler to deduce T from the argument type, so both explicit and implicit type specification work.

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