Java Cross-Concept Problems: Generics, Polymorphism, Collections and Exceptions
Learn a fixed reading order for Java problems that mix declared types, runtime dispatch, generic wildcards, list mutations, comparator sorting and exception control flow.
KnowledgeGate Team
Exam prep & CS education

Java questions combine inheritance, generics, collections and exception flow. One short program can compile against one reference type, dispatch by runtime type, mutate a list after failure and ask for exact output. The reliable method is to check compilation, track runtime objects, trace collection state, follow exceptional control flow and then write each line. Use Coding & DSA Courses for Placements as the broader practice route.
Java cross-concept questions have five layers
Separate five overlapping layers. The declared reference type controls callable members at compile time. The runtime class chooses an overridden instance method. A generic type or wildcard controls what a collection reference may read or write. Collection operations preserve or change object order. An exception abandons the current expression and transfers control to a matching catch block.
For foundations, read OOPs Concepts in Java: Worked Interview Questions for inheritance and dispatch. The Polymorphism in Java: Overloading, Overriding and Runnable Examples article isolates overload and override dispatch; the worked example here combines dispatch with generics, list mutation and exception flow.
Java object types and the initial list state
The abstract Material class has immutable code and raw fields plus an abstract score() method. Note.score() adds a bonus. Quiz.score() subtracts a penalty, using Math.max to prevent a negative result.
Index | Declared list element type | Runtime object | Fields | Score if called |
|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
| not called because validation throws first |
All elements are readable as Material, but dynamic dispatch selects Note.score() or Quiz.score() from the actual object. N2 is invalid: its negative raw value is checked before its override runs.

Worked Java trace: validation, dispatch, removal and sorting
The following program combines these rules:
import java.util.*;
abstract class Material {
private final String code;
private final int raw;
Material(String code, int raw) {
this.code = code;
this.raw = raw;
}
String code() { return code; }
int raw() { return raw; }
abstract int score();
}
final class Note extends Material {
private final int bonus;
Note(String code, int raw, int bonus) {
super(code, raw);
this.bonus = bonus;
}
@Override int score() { return raw() + bonus; }
}
final class Quiz extends Material {
private final int penalty;
Quiz(String code, int raw, int penalty) {
super(code, raw);
this.penalty = penalty;
}
@Override int score() { return Math.max(0, raw() - penalty); }
}
public class CrossConceptDemo {
static int totalOf(List<? extends Material> items) {
int total = 0;
for (Material item : items) {
if (item.raw() < 0) {
throw new IllegalArgumentException(item.code());
}
total += item.score();
}
return total;
}
public static void main(String[] args) {
List<Material> materials = new ArrayList<>();
materials.add(new Note("N1", 12, 3));
materials.add(new Quiz("Q1", 10, 4));
materials.add(new Note("N2", -5, 2));
try {
System.out.println("First total=" + totalOf(materials));
} catch (IllegalArgumentException ex) {
System.out.println("Rejected=" + ex.getMessage());
materials.removeIf(m -> m.code().equals(ex.getMessage()));
}
materials.sort(
Comparator.comparingInt(Material::score).reversed());
materials.forEach(m ->
System.out.println(m.code() + "=" + m.score()));
System.out.println("Total=" + totalOf(materials));
}
}Trace the first totalOf from total=0. N1 passes validation; Note.score() gives 12+3=15, so total becomes 15. Q1 passes; Quiz.score() gives max(0,10-4)=6, making total 15+6=21. N2 fails raw<0, so the method throws IllegalArgumentException("N2") before N2's score() and never returns the partial total.
Java evaluates the concatenation before the first println. Because totalOf throws, no First total=... line appears. The catch prints Rejected=N2, and removeIf changes [N1,Q1,N2] to [N1,Q1]. Sort keys 15 and 6 leave descending order as N1, Q1. The second totalOf returns 15+6=21.
Rejected=N2
N1=15
Q1=6
Total=21The first visible line comes from the catch. After removal, forEach evaluates score() again and prints N1=15 and Q1=6. The final println calls totalOf on the remaining list: N1 moves its new local total from 0 to 15, Q1 moves it from 15 to 21, and 21 is returned. Sorting changes positions, not objects or scores. The abandoned partial total and final returned total both equal 21, but only the latter escapes its call. Output tracing must therefore follow control flow, not merely collect arithmetic results.

Java generics and polymorphism solve different parts of the program
List<Note> is not a subtype of List<Material>, although Note is a subtype of Material. Otherwise, code receiving List<Material> could insert a Quiz into a notes-only list. List<? extends Material> lets totalOf accept List<Material>, List<Note> or List<Quiz> while reading each element as Material.
Inside totalOf, items.add(new Note("N3", 8, 1)) does not compile. The captured type might be Quiz, so adding a Note is unsafe. Only null is generally addable, and this method should not add it. That is a compile-time generic restriction. Once read as Material, runtime polymorphism chooses the actual score() implementation: 15 for N1 and 6 for Q1. Generics decide access safety; polymorphism decides which implementation runs.
Java exceptions and collection operations change what runs next
IllegalArgumentException is unchecked, but the matching catch handles it. Throwing abandons totalOf, the pending concatenation and the first println. Local total 21 is lost because the method never returns it.
removeIf is valid because the enhanced for loop has unwound. It runs afterward on the ArrayList, outside the loop's iterator, and removes only the element coded N2. Next, Material::score extracts keys N1=15 and Q1=6. comparingInt sorts ascending, while reversed() makes it descending..
Java cross-concept traps that produce the wrong output
Partial-result trap: Printing
First total=21assumes processed values survive exception unwinding. Correction: the first print never runs because the method throws before returning.Static-dispatch trap: Treating
score()as one abstract-base implementation misses overriding. Correction: use the runtimeNoteorQuizmethod.Generic-subtyping trap: Treating
List<Note>asList<Material>would permit aQuizin a notes-only list. Correction: useList<? extends Material>for this producer-style view.Mutation trap: Removing N2 directly from
materialsduring the enhanced loop risksConcurrentModificationException. Correction: this program removes it later withremoveIf.Sorting trap: Ignoring
reversed()predicts Q1 before N1. Correction: compute keys, then apply the direction.Validation-order trap: Computing N2 as
-5+2=-3ignores the earlier check. Correction:raw<0throws before dispatch.
Java assessment questions combine these concepts
Mixed Java problems may ask for output, the first exception, wildcard compilation, runtime override or comparator order. Apply the same layered method to each.
Check A: Add
items.add(new Note("N3",8,1))insidetotalOf. It is a compile-time error under? extends Materialbecause the captured subtype is unknown.Check B: Remove
.reversed()after N2 is caught. Ascending order printsQ1=6beforeN1=15, while the final total remains 21.Check C: Change only N2's raw value from -5 to 5. Validation passes, N2 scores
5+2=7, and the first total is15+6+7=28. The output beginsFirst total=28, followed byN1=15,N2=7,Q1=6, thenTotal=28.
Use additional Java practice after tracing the program; mixed questions are easiest when each answer states the compile-time type, runtime dispatch and collection state.
Java cross-concept problems: the short version
Use five checks: verify compilation, label declared and runtime types, freeze collection state, follow normal or exceptional jumps, then write output after mutation and sorting. Here, N1 and Q1 build partial 21, N2 prevents the return, the catch removes N2, and the final total is 21. Continue with the Java Course: Concepts, MCQs & Coding Questions, or apply Java to data structures through DSA using Java, Placement Preparation Course.
Keep learning
Java Interview Questions for Freshers: How to Answer OOPs, Strings, Collections and Exceptions
Prepare the Java questions freshers actually face, then learn to explain each output instead of stopping at a memorised definition.
Java Interview Questions for Freshers: Collections, Exceptions and JVM Traces
Practise Java interviews by predicting exact outputs, tracing object and collection state, following exception paths, and explaining JVM memory without slogans.
Generics in Java: Type Parameters, Bounds, Wildcards and Runnable Examples
Build type-safe Java containers, then trace generic methods, upper bounds, wildcard reads and writes, invariance, erasure, and common failure cases.
Collections Framework in Java: Interfaces, Implementations, and Examples
Learn how List, Set, Map, and Deque fit together, then choose implementations confidently through one consistent Java example.





