Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 91–100 of 476
Topics in Java Programming
Q.91 Medium Generics
What is the correct output of this code?
java
List
A Compilation successful
B Compilation error
C Runtime exception
D Adds 5 to the list
Correct Answer:  B. Compilation error
EXPLANATION

With upper-bounded wildcard (? extends Number), you can read but cannot add elements (except null) because the compiler doesn't know the exact type.

Take Test
Q.92 Medium Generics
Consider the code:
java
List list = new ArrayList();

Will this compile?
A Yes, Integer is a subtype of Number
B No, ArrayList is not compatible with List
C Yes, with warning
D Depends on compiler version
Correct Answer:  B. No, ArrayList is not compatible with List
EXPLANATION

Generics are invariant. ArrayList<Integer> cannot be assigned to List<Number> even though Integer is a subtype of Number. This prevents type safety issues.

Take Test
Q.93 Medium Generics
What does the following generic declaration mean?
public T findMax(T a, T b)
A T must be Number or Comparable
B T must extend Number and implement Comparable
C T must be Number and a subclass of Comparable
D T can be either Number or Comparable
Correct Answer:  B. T must extend Number and implement Comparable
EXPLANATION

Multiple bounds in generics use '&' to specify that the type must satisfy all constraints. T must extend Number AND implement Comparable<T>.

Take Test
Q.94 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.95 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.96 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.97 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.98 Medium Generics
Consider:
public class Pair { }
Which declaration is invalid?
A Pair pair = new Pair();
B Pair pair = new Pair();
C Pair pair = new Pair();
D Pair
Correct Answer:  D. Pair
EXPLANATION

Option D is invalid because we cannot instantiate a generic class with wildcard type parameters. Wildcards are only for variable declarations, not for instantiation with 'new'.

Take Test
Q.99 Medium Generics
What is the output of this code?
List strings = new ArrayList();
List raw = strings;
raw.add(123);
String str = strings.get(0);
A Compiles and runs without error
B Compile-time error: cannot add Integer to List
C Runtime ClassCastException when retrieving the element
D Compile-time error: raw type cannot be assigned
Correct Answer:  C. Runtime ClassCastException when retrieving the element
EXPLANATION

The code compiles (with warnings about raw types) but throws ClassCastException at runtime when trying to cast Integer to String. Type safety is lost with raw types.

Take Test
Q.100 Medium Generics
Given: Map
A map.put("key", new ArrayList())
B List list = map.get("key"); list.add("value");
C List list = map.get("key");
D map.put("key", Arrays.asList(1, 2, 3))
Correct Answer:  C. List list = map.get("key");
EXPLANATION

We can read from map.get() and assign to List<?>, but cannot add to List<?> or put into the map due to wildcard restrictions. Option C only reads the value.

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