Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Exceptions and Generics

    Interview Questions

    Real interview questions on Exceptions and Generics, answered the way you would say them out loud.

    Extra: Interview prep5 min read

    Interview questions

    What's the difference between checked and unchecked exceptions? Which do you prefer?

    How I'd answer: "Checked exceptions extend Exception but not RuntimeException, and the compiler forces callers to catch them or declare them with throws — they're meant for expected, recoverable conditions outside the program's control, like a missing file or network failure. Unchecked exceptions extend RuntimeException and aren't enforced — they usually indicate programming errors like a null reference or an illegal argument. Errors like OutOfMemoryError are a separate branch you normally don't catch.

    In practice I use checked exceptions sparingly. They make failure explicit in the contract, but they propagate throws clauses through every layer and don't work well with lambdas. A common approach — and what Spring does — is to catch checked exceptions at the boundary where they occur and wrap them in a meaningful unchecked exception with the original as the cause, then handle them centrally, for example turning them into HTTP responses."

    Combines: Ch 3, 5.

    Does finally always execute?

    How I'd answer: "In practice yes — after normal completion, after a caught or uncaught exception, and even after a return in the try or catch block. It doesn't run if the JVM exits via System.exit, crashes or is killed, or if the try block never finishes. One trap: a return inside finally overrides the try's return value and silently discards any exception in flight, so you should never return from finally. For closing resources, I'd use try-with-resources rather than hand-written finally blocks."

    Combines: Ch 2, 4.

    What is type erasure, and what limitations does it cause?

    How I'd answer: "Java generics are a compile-time feature: the compiler checks the types, then erases them — replacing type parameters with Object or their bound and inserting casts. At run time List<String> and List<Integer> are both just ArrayList; I can verify with getClass(). It was done for backward compatibility with pre-generics code. The consequences: you can't instantiate a type parameter with new T(), can't create generic arrays, can't use instanceof with a parameterized type, can't use primitives as type arguments — so List<int> becomes List<Integer> with boxing — and you can't overload methods that differ only in type arguments, since they erase to the same signature."

    Combines: Ch 7, 9.

    Explain ? extends and ? super. What's PECS?

    How I'd answer: "Generics are invariant — List<Integer> isn't a List<Number> — because otherwise you could add a Double into a list of Integers through the wider reference. Wildcards give safe flexibility. List<? extends Number> means a list of some subtype of Number: I can read Numbers from it but can't add, so it's for producers. List<? super Integer> means a list of Integer or a supertype: I can add Integers but reading only gives Object, so it's for consumers. PECS — producer extends, consumer super — is the rule, and Collections.copy(List<? super T> dest, List<? extends T> src) is the textbook example. If a parameter is both read and written, I just use List<T>."

    Combines: Ch 10.

    How would you handle errors when reading a quiz file and saving results to a database?

    How I'd answer: "I'd open both the file reader and the database connection with try-with-resources so they're always closed, even on failure. At the storage layer I'd catch the checked IOException and SQLException and wrap them in meaningful unchecked exceptions like QuizStorageException, passing the original as the cause so the stack trace shows the root problem. I'd validate inputs up front and fail fast with IllegalArgumentException for bad data. Higher up, at the API boundary, one central handler would translate these into proper responses — a 404 for a missing quiz, a 503 if the database is down — and log the full stack trace with context like the quiz ID and user. I wouldn't swallow anything, and I wouldn't use exceptions for expected flow like validating a user's answer — I'd check it directly."

    Combines: Ch 2–5; Networking 3: DNS, HTTP and HTTPS Ch 5.