Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 181–190 of 958
Topics in Java Programming
Q.181 Easy Generics
Which of the following correctly declares a generic method that returns the first element of any collection?
A public T getFirst(Collection collection)
B public T getFirst(Collection collection)
C public T getFirst(Collection collection)
D public Collection getFirst(T collection)
Correct Answer:  B. public T getFirst(Collection collection)
EXPLANATION

Generic methods require type parameter declaration before return type. The collection parameter must be of type Collection<T> to extract element of type T.

Take Test
Q.182 Easy Generics
What will be the output of the following code?
java
List list = new ArrayList();
list.add("Java");
Object obj = list;
System.out.println(list.getClass() == obj.getClass());
A true
B false
C Compilation error
D Runtime exception
Correct Answer:  A. true
EXPLANATION

Generic type information is erased at runtime. Both list and obj refer to the same ArrayList class, so getClass() returns the same class object.

Take Test
Q.183 Hard Generics
How would you correctly use generics in a recursive type bound scenario?
A class Node { Node next; }
B class Node { Node next; }
C class Node extends T { }
D class Node { }
Correct Answer:  A. class Node { Node next; }
EXPLANATION

Option A demonstrates recursive type bound (also called F-bounded polymorphism), which allows a type to reference itself in its bound. This is commonly used in builder patterns and comparators.

Take Test
Q.184 Medium Generics
In the context of generics, what does 'invariance' mean?
A A generic type List is a subtype of List
B A generic type List is not a subtype of List, despite Integer being a subtype of Number
C Invariance allows covariance and contravariance together
D Invariance means the type cannot change
Correct Answer:  B. A generic type List is not a subtype of List, despite Integer being a subtype of Number
EXPLANATION

Generics are invariant in Java. List<Integer> is NOT a subtype of List<Number> to maintain type safety. This is a key feature preventing runtime errors.

Take Test
Q.185 Easy Generics
What is the relationship between generics and arrays in Java?
A Generic arrays can be created freely like List[]
B Generic arrays are not supported; you cannot create arrays of parameterized types
C Generic arrays work only with unbounded wildcards
D Arrays of generics work only for primitive types
Correct Answer:  B. Generic arrays are not supported; you cannot create arrays of parameterized types
EXPLANATION

Java does not allow creation of arrays with parameterized types due to type erasure. List<String>[] would be unsafe. You must use List<String>[] or List[] instead.

Take Test
Q.186 Hard Generics
Which of the following represents valid bounded wildcard usage for a method that processes collections?
A public void process(Collection
B public void process(Collection
C public void process(Collection col) { col.add(5); }
D public void process(Collection col) { col.add(5); }
Correct Answer:  B. public void process(Collection
EXPLANATION

Only option B allows adding elements safely. '? super Integer' means the collection can hold Integer or any supertype. Option A prevents adding, Option C prevents adding, Option D restricts to exactly Number.

Take Test
Q.187 Medium Generics
What happens when you try to cast a generic object?
List strings = (List) getSomeList();
A The cast is fully checked at compile time
B The cast is unchecked; generic type information is lost due to erasure
C The cast always succeeds because of type erasure
D A compile-time error is raised
Correct Answer:  B. The cast is unchecked; generic type information is lost due to erasure
EXPLANATION

Due to type erasure, casts involving generic types are unchecked. The compiler warns about this but allows it. The actual cast check only verifies the raw type.

Take Test
Q.188 Medium Generics
Given the declaration: List listOfLists;
Which assignment is valid?
A listOfLists = new ArrayList();
B listOfLists = new ArrayList
C listOfLists = new ArrayList();
D listOfLists = new ArrayList();
Correct Answer:  D. listOfLists = new ArrayList();
EXPLANATION

Nested generics must match exactly or use appropriate wildcards. Only option D provides exact type match. Option B would work with wildcards, but that's not listed correctly here.

Take Test
Q.189 Medium Generics
What is the correct way to declare a generic method that accepts a list and returns the maximum element?
A public T getMax(List list) { ... }
B public static T getMax(List
C public
D public T getMax(List list) { ... }
Correct Answer:  D. public T getMax(List list) { ... }
EXPLANATION

Option D correctly ensures T is comparable with itself using F-bounded polymorphism. Option C has inverted logic with '? super'. Option B is unnecessarily complex.

Take Test
Q.190 Hard Generics
What is the compiled signature of this generic method?
public void sort(T[] array)
A public void sort(Number[] array)
B public void sort(Comparable[] array)
C public void sort(Object[] array)
D Type erasure uses the first bound: public void sort(Number[] array)
Correct Answer:  D. Type erasure uses the first bound: public void sort(Number[] array)
EXPLANATION

With multiple bounds using '&', type erasure replaces T with the first bound. Here, Number is the first bound, so T is replaced with Number.

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