Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 121–130 of 476
Topics in Java Programming
Q.121 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'.

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

Take Test
Q.123 Medium Generics
Which of these declarations would NOT cause a compilation warning or error?
A List list = new ArrayList();
B List list = new ArrayList();
C List list = new ArrayList();
D List list = new ArrayList();
Correct Answer:  A. List list = new ArrayList();
EXPLANATION

Option A uses proper diamond syntax with matching type parameters on both sides. Options B, C produce unchecked warnings, and Option D is invalid due to invariance.

Take Test
Q.124 Medium Generics
What will be the output?
List list = new ArrayList();
list.add(10);
Object obj = list.get(0);
System.out.println(obj instanceof Integer);
A true
B false
C Compilation error
D Runtime exception
Correct Answer:  A. true
EXPLANATION

The Integer value is stored in the list. At runtime, type erasure converts it to Object, but the actual stored value remains an Integer instance.

Take Test
Q.125 Medium Generics
Consider this generic interface:
interface Comparable { int compareTo(T o); }
Which class definition correctly implements this?
A class MyClass implements Comparable { public int compareTo(MyClass o) { return 0; } }
B class MyClass implements Comparable { public int compareTo(Object o) { return 0; } }
C class MyClass implements Comparable { public int compareTo(Object o) { return 0; } }
D class MyClass implements Comparable { public int compareTo(T o) { return 0; } }
Correct Answer:  A. class MyClass implements Comparable { public int compareTo(MyClass o) { return 0; } }
EXPLANATION

Option A correctly implements the generic interface by specifying the concrete type parameter as MyClass in the implements clause.

Take Test
Q.126 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.127 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.128 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.129 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.130 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
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