Home Subjects Java Programming Generics

Java Programming
Generics

Java OOP, collections, multithreading

26 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–26 of 26
Topics in Java Programming
Q.21 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.

Test
Q.22 Hard Generics
What will be the result of this code?
List list = new ArrayList();
list.add("Hello");
List raw = list; // Unchecked assignment
raw.add(123); // Adding Integer to raw type
String s = list.get(1);
A ClassCastException when accessing list.get(1)
B Code executes without error
C Compilation error
D StringIndexOutOfBoundsException
Correct Answer:  A. ClassCastException when accessing list.get(1)
EXPLANATION

Raw type assignment bypasses generics. Integer 123 is added to the list. When casting to String at get(1), ClassCastException occurs.

Test
Q.23 Hard Generics
Which statement is true about generic inheritance?
A ArrayList is NOT a subtype of List due to invariance
B ArrayList IS a subtype of List
C ArrayList is a subtype of ArrayList
D Generic types support covariance by default
Correct Answer:  B. ArrayList IS a subtype of List
EXPLANATION

ArrayList<Integer> is a subtype of List<Integer> because ArrayList is a subclass of List with the same type parameter.

Test
Q.24 Hard Generics
Consider this code:
public T getMax(T a, T b) {
return a.compareTo(b) > 0 ? a : b;
}
What is the benefit of this recursive bound ?
A Ensures type safety by guaranteeing T implements Comparable with itself
B Allows T to be compared with any type
C Improves runtime performance
D Eliminates the need for type erasure
Correct Answer:  A. Ensures type safety by guaranteeing T implements Comparable with itself
EXPLANATION

Recursive bound <T extends Comparable<T>> ensures that T implements Comparable interface specifically for comparing with its own type, providing type safety.

Test
Q.25 Hard Generics
What will happen with this code?
List list = new ArrayList();
list.add(123); // Adding Integer
String s = (String) list.get(0);
A ClassCastException at runtime
B Compilation error
C Code executes successfully
D Unchecked warning at compile time only
Correct Answer:  A. ClassCastException at runtime
EXPLANATION

Raw type List accepts any object. At runtime, the Integer 123 cannot be cast to String, causing ClassCastException.

Test
Q.26 Hard Generics
Which of these correctly demonstrates the Producer Extends Consumer Super (PECS) principle?
A public void process(List
B public void process(List
C public void process(List source, List dest) { }
D public void process(List source, List dest) { }
Correct Answer:  A. public void process(List
EXPLANATION

PECS principle: use 'extends' when reading from a collection (source), use 'super' when writing to it (dest). Option A reads from source and writes to dest correctly.

Test
IGET
IGET AI
Online · Exam prep assistant
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