Java Programming — Generics
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 11–20 of 100 questions in Generics
Q.11 Hard Generics
What is the compiled bytecode signature of this generic method?
public T findMin(T[] arr)
A public Comparable findMin(Comparable[] arr)
B public Object findMin(Object[] arr)
C public Comparable findMin(Comparable[] arr)
D public Comparable findMin(Comparable[] arr) with bridge method
Correct Answer:  A. public Comparable findMin(Comparable[] arr)
EXPLANATION

Type erasure replaces T with its bound Comparable. The method signature becomes public Comparable findMin(Comparable[] arr).

Take Test
Q.12 Hard Generics
What is the difference between List and List in practical usage?
A They are identical
B List can accept any type; List cannot accept anything
C List can add Objects; List can only add null
D List is deprecated in favor of List
Correct Answer:  C. List can add Objects; List can only add null
EXPLANATION

List<Object> accepts Object and its subclasses for adding. List<?> (unbounded wildcard) is more flexible for reading but restrictive for writing (only null).

Take Test
Q.13 Hard Generics
Consider this declaration: public static List createList(). Which statement about type inference is correct?
A Compiler always knows the type T
B Type T is inferred from context where method is called
C Type T must always be explicitly specified
D Compiler defaults T to Object
Correct Answer:  B. Type T is inferred from context where method is called
EXPLANATION

Java's type inference deduces T from the assignment context. E.g., 'List<String> list = createList()' infers T as String.

Take Test
Q.14 Hard Generics
What will happen when you compile and run this code?
java
List strings = Arrays.asList("a", "b", "c");
List raw = strings;
raw.add(123);
String s = strings.get(3);
A Compiles successfully and runs without error
B Compilation error due to raw type usage
C Compiles with warning, throws ClassCastException at runtime
D Compiles and throws ArrayIndexOutOfBoundsException
Correct Answer:  C. Compiles with warning, throws ClassCastException at runtime
EXPLANATION

Assigning generic type to raw type suppresses type checking. The Integer added bypasses the type system, causing ClassCastException when retrieved as String.

Take Test
Q.15 Medium Generics
Which of these is a valid generic interface implementation?
A class MyClass implements Comparable
B class MyClass implements Comparable
C class MyClass extends Comparable
D class MyClass implements Comparable
Correct Answer:  A. class MyClass implements Comparable
EXPLANATION

Implementing generic interfaces requires specifying the type parameter. 'implements Comparable<MyClass>' is the proper way to implement.

Take Test
Advertisement
Q.16 Medium Generics
What is the PECS principle in Java Generics?
A Public Extends Collections Structure
B Producer Extends, Consumer Super
C Parameter Extends Comparable Supports
D Polymorphic Extends Class Super
Correct Answer:  B. Producer Extends, Consumer Super
EXPLANATION

PECS (Producer Extends, Consumer Super) is a guideline: use 'extends' for producers (reading) and 'super' for consumers (writing).

Take Test
Q.17 Medium Generics
What does 'covariance' mean in the context of Java Generics?
A Types vary together in the same direction
B Types vary in opposite directions
C Types do not vary at all
D Types are interchangeable
Correct Answer:  A. Types vary together in the same direction
EXPLANATION

Covariance means if Dog is a subtype of Animal, then Producer<Dog> is a subtype of Producer<Animal>. Achieved using upper-bounded wildcards.

Take Test
Q.18 Medium Generics
Which wildcard usage follows the Producer pattern?
A List
B List
C List
D List
Correct Answer:  B. List
EXPLANATION

Producer (Get-Only pattern) uses upper-bounded wildcard (? extends T). It's used when you want to read/produce elements of specific types.

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