Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 221–230 of 958
Topics in Java Programming
Q.221 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.222 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.223 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.224 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.225 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.226 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.227 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.228 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.229 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.230 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
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