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 31–40 of 49
Topics in Java Programming
Q.31 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.

Test
Q.32 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.

Test
Q.33 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.

Test
Q.34 Medium Generics
What is the relationship between raw types and generics in Java?
A Raw types are legacy code that bypasses generic type checking
B Raw types are automatically converted to generic types
C Raw types and generics cannot coexist
D Raw types are more efficient than generics
Correct Answer:  A. Raw types are legacy code that bypasses generic type checking
EXPLANATION

Raw types (like 'List' without type parameters) are legacy and should be avoided as they bypass compile-time type safety.

Test
Q.35 Medium Generics
Which generic wildcard usage is INCORRECT?
A List
B List
C Map map;
D List
Correct Answer:  D. List
EXPLANATION

Nested wildcards like '? extends ? super T' are not allowed. Wildcards cannot be combined or nested.

Test
Q.36 Medium Generics
In a generic class 'class Box', what constraint is placed on T?
A T must implement the Comparable interface with itself as the type parameter
B T must be a subclass of Comparable
C T must be Comparable with any type
D T is unrestricted and Comparable is optional
Correct Answer:  A. T must implement the Comparable interface with itself as the type parameter
EXPLANATION

The bound 'T extends Comparable<T>' ensures T is comparable to itself, enabling type-safe comparison operations.

Test
Q.37 Medium Generics
What does the PECS principle stand for in generics context?
A Producer Extends, Consumer Super
B Parameter Extends, Class Super
C Polymorphic Extends, Constructor Super
D Public Extends, Static Super
Correct Answer:  A. Producer Extends, Consumer Super
EXPLANATION

PECS guides when to use upper bounds (? extends) for producers/readers and lower bounds (? super) for consumers/writers.

Test
Q.38 Medium Generics
Which generic declaration would allow storing both String and Integer in the same collection?
A List list = new ArrayList();
B List list = new ArrayList();
C List
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

All three allow storing multiple types because they use unbounded or Object-level wildcards, though List<?> is most restrictive for additions.

Test
Q.39 Medium Generics
Which statement correctly uses the lower-bounded wildcard in generics?
A List
B List
C List list = new ArrayList();
D List list = new ArrayList();
Correct Answer:  A. List
EXPLANATION

Lower-bounded wildcards (? super T) allow superclasses of T. This assignment is valid because ArrayList<Number> fits '? super Integer'.

Test
Q.40 Medium Generics
In the declaration 'List
A Any Number subclass
B Only Number or its exact subclasses
C Nothing can be added safely (except null)
D Only Integer and Double
Correct Answer:  C. Nothing can be added safely (except null)
EXPLANATION

Upper-bounded wildcards (? extends T) are used for reading data. Adding elements is unsafe because the compiler doesn't know the exact type.

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