Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 111–120 of 476
Topics in Java Programming
Q.111 Medium Generics
Which statement correctly defines a generic class with multiple type parameters and bounds?
A class Container { }
B class Container extends Number { }
C class Container { }
D class Container extends { }
Correct Answer:  C. class Container { }
EXPLANATION

Multiple type parameters with bounds must be comma-separated and each can have its own upper bound. Option C shows correct syntax with recursive bound on T and bound on K.

Take Test
Q.112 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.113 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.114 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.115 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
Q.116 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.

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

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

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

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

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