Java Generics Interview Questions 2026: 26 Q&A With Code
26 Java generics interview questions for 2026 with full answers, type erasure, bounded types, wildcards, PECS and predict-the-output snippets. Candidate-reported backend and product-company favourites.
on this page§ 09
Last Updated: June 2026 | Level: Freshers to Mid-Level | Read Time: ~15 min
Generics are where Java interviews probe whether you understand the type system, not just the syntax. Candidates report that type erasure, wildcards, and the PECS principle are guaranteed for backend and product roles. This guide covers 26 generics questions with full answers and predict-the-output traps. Behaviour reflects the official Oracle Java tutorials; confirm any type-system detail on the JLS.
Pair this with Java Interview Questions 2026 and Java Collections Interview Questions 2026.
Table of Contents
- Fundamentals (Q1 to Q9)
- Type Erasure (Q10 to Q15)
- Wildcards and Bounds (Q16 to Q22)
- Advanced (Q23 to Q26)
- Predict the Output
- Quick Revision Table
- Frequently Asked Questions
Fundamentals
Q1. What are generics in Java? Easy
List<String> names = new ArrayList<>();
names.add("Alice");
String s = names.get(0); // no cast needed
Q2. Why were generics introduced? Easy
Q3. What is a generic class? Medium
class Box<T> {
private T value;
void set(T v) { value = v; }
T get() { return value; }
}
Q4. What is a generic method? Medium
static <T> T firstOf(List<T> list) { return list.get(0); }
Q5. What are the naming conventions for type parameters? Easy
Q6. Can you use primitives as type arguments? Medium
Q7. What is the diamond operator? Easy
Q8. What is a bounded type parameter? Medium
Q9. Can a generic type have multiple bounds? Hard
Type Erasure
Q10. What is type erasure? Hard
Q11. Why can you not use instanceof with a parameterised type? Hard
Q12. Why can you not create a generic array? Hard
Q13. Can two methods differ only by generic type? Hard
Q14. What is a raw type? Medium
Q15. Can you reference a static field of type parameter T? Medium
Wildcards and Bounds
Q16. What is a wildcard in generics? Medium
Q17. What is an upper-bounded wildcard? Medium
double sum(List<? extends Number> nums) {
double s = 0;
for (Number n : nums) s += n.doubleValue();
return s;
}
Q18. What is a lower-bounded wildcard? Medium
Q19. What is the PECS principle? Hard
Q20. What is the difference between List<Object> and List<?>? Hard
Q21. Why can you not add to a List<? extends Number>? Hard
Q22. What is an unbounded wildcard good for? Medium
Advanced
Q23. What is a recursive generic bound? Hard
Q24. Can a generic class extend another generic class? Medium
Q25. What are bridge methods? Hard
Q26. Are generics covariant like arrays? Hard
Predict the Output
Snippet 1
List<Integer> ints = new ArrayList<>();
List<String> strs = new ArrayList<>();
System.out.println(ints.getClass() == strs.getClass());
Output:
true
Why: Type erasure means both are just ArrayList at runtime, so their classes are identical.
Snippet 2
List<? extends Number> list = new ArrayList<Integer>();
// list.add(1); // compile error
Number n = list.get(0);
Output:
(compiles only without the add line)
Why: An upper-bounded wildcard is read-only for the element type; add is rejected, get returns Number.
Snippet 3
static <T> T pick(T a, T b) { return a; }
System.out.println(pick("x", 5).getClass().getSimpleName());
Output:
Object
Why: With mixed arguments the inferred T is the common supertype Object (technically Serializable & Comparable), so the returned reference type is Object-like; the actual object returned is the String "x".
Quick Revision Table
| Concept | Key takeaway |
|---|---|
| generics | compile-time type safety |
| type erasure | no type info at runtime |
| no generic array | erasure prevents it |
| extends wildcard | read (producer) |
| super wildcard | write (consumer) |
| PECS | producer extends, consumer super |
| invariance | List<Integer> not a List<Object> |
Frequently Asked Questions
What generics topic is asked most in Java interviews in 2026?
Candidates report that type erasure, the difference between extends and super wildcards (PECS), and why you cannot create a generic array are the three most repeated Java generics questions.
Is type erasure important for interviews?
Yes. Understanding that generics are a compile-time feature erased at runtime explains many restrictions, like why you cannot use instanceof with a parameterised type or create new T.
What does PECS mean?
Producer Extends, Consumer Super. Use extends when a structure produces values you read, and super when it consumes values you write. It guides correct wildcard use in APIs.
Why are generics invariant when arrays are covariant?
Array covariance defers type checking to runtime and can throw ArrayStoreException, while generic invariance catches the same mismatch at compile time. Wildcards let you opt into controlled variance when you need it.
Why can I not add to a List<? extends Number>?
Because the list might actually be a List<Integer> or List<Double>, and the compiler cannot guarantee the value you add matches the real element type, so it forbids all adds except null.
Related Articles
- Java Interview Questions 2026, the master 50-question set
- Java Collections Interview Questions 2026, generics power collections
- Java OOPs Interview Questions 2026, object model
- Java Output Prediction Questions 2026, tricky snippets
Confirm any type-system detail on the official Oracle Java documentation before your interview. This guide reflects candidate-reported patterns and public preparation resources as of June 2026.
Sources and review notesreviewed 8 Jun 2026
Official notices, candidate reports, offer documents, and editorial practice questions carry different confidence levels. The visible source list lets you inspect the evidence instead of relying on a blanket verification badge.
topic cluster
More resources in Interview Questions
Use the category hub to browse similar questions, exam patterns, salary guides, and preparation resources related to this topic.
paid contributor programme
Sat this this year? Share your story, earn ₹500.
First-person experience reports help future candidates prep smarter. We pay verified contributors ₹500 via UPI per accepted story with byline.
Submit your story →ready to practice?
Take a free timed mock test
Put what you learned into practice. Our mock tests match the 2026 pattern with timer, navigator, reveal, and score breakdown. No signup.