Core Java APIs· 40 min

Exceptions, Lambdas, and Streams

Connect checked exceptions, custom exceptions, functional interfaces, streams, collectors, and Optional.

Java interview lesson
Question 6 · Not reviewed

Core APIs · behavior

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

Exceptions, Lambdas, and Streams

Explain it aloud in your own words

Strong answer

Exceptions represent failure or exceptional control flow; checked exceptions communicate recoverable obligations while unchecked exceptions usually indicate programming or environment failures. Lambdas provide behavior as values, and streams describe a pipeline of transformations that is evaluated lazily until a terminal operation runs.

Key points

  • Do not catch Exception broadly unless you can recover or add useful context.
  • A stream is not a collection and should not be reused after a terminal operation.
  • Avoid side effects inside parallel or shared stream pipelines.
  • Optional is best used for return values, not as a field or method parameter everywhere.

Interviewer looks for

Show that you understand lazy evaluation, terminal operations, exception boundaries, and when a plain loop is clearer.

Java example

1List<String> result = names.stream()
2  .filter(name -> name.length() > 3)
3  .map(String::toUpperCase)
4  .toList();

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Intermediate

What is the difference between checked and unchecked exceptions?

Strong answer

Checked exceptions must be caught or declared and communicate an obligation to handle a recoverable condition. Unchecked exceptions extend RuntimeException and commonly represent programming errors or failures callers cannot reasonably recover from.

Q2Intermediate

What happens if both try and finally return a value?

Strong answer

A return in finally overrides a return from try or catch and also hides exceptions, which makes control flow difficult to reason about. Returning from finally should be avoided.

Q3Basic

What makes an interface functional?

Strong answer

A functional interface has exactly one abstract method, so it can be implemented with a lambda or method reference. It may still contain default and static methods; @FunctionalInterface asks the compiler to verify the contract.

Q4Intermediate

Why are streams described as lazy?

Strong answer

Intermediate operations such as filter and map build a pipeline without processing elements immediately. A terminal operation such as toList or count triggers traversal, often allowing fusion and short-circuiting.

Q5Intermediate

What is the difference between map and flatMap?

Strong answer

map transforms each input into one output, while flatMap transforms each input into a stream and flattens those streams into one pipeline. flatMap is useful for nested collections or optional-like results.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.