Java Lambdas Quiz: Inner Classes and Method References Test
Practise Java nested classes and lambdas with 12 questions and two programs on inner and anonymous classes, effectively final captures and method references.
- Course: Java study plan
- Module: Nested classes & lambdas
- Kind: Checkpoint — cleared at 70%
- Reading time: 22 min
- Runtime: OpenJDK 18
Checkpoint — Nested classes & lambdas is the checkpoint that closes the Nested classes & lambdas module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers static nested versus inner classes, local and anonymous classes, lambda syntax and semantics, the four kinds of method reference, and functions as values.
How it works. Twelve questions and two programs; 70% on the questions and both programs accepted clears the module.
Before you start, make sure you can answer:
- What an inner class instance holds that a static nested one does not, and why that leaks.
- Why captured locals must be effectively final.
- What
thismeans inside a lambda versus inside an anonymous class. - Which kind of reference
String::lengthis, and whatprefix::startsWithcaptures. - How to return a function that remembers an argument.
The programs are a higher-order text pipeline built from composed functions, and an iterator implemented as an inner class alongside a lambda-based strategy table.
Common questions
What does an inner class instance hold that a static nested class does not?
A hidden reference to its enclosing outer instance, Outer.this. It lets the inner object read the outer's fields, and it also keeps the outer object reachable for as long as the inner one is — a common memory leak.
What kind of method reference is String::length?
An unbound instance method reference: the first parameter of the target function becomes the receiver, so it means s -> s.length(). By contrast prefix::startsWith is bound — it captures the prefix object when the reference is created.
What does this mean inside a lambda compared with an anonymous class?
In a lambda this is the enclosing instance, because a lambda introduces no new this of its own. In an anonymous class this is the anonymous object itself.
How do you return a function that remembers an argument in Java?
Return a lambda that captures the method's parameter, as startsWith(String prefix) returning s -> s.startsWith(prefix) does. Each call yields a closure holding its own prefix.
Exercises
Event bus with lambdas
Write class EventBus with subscribe(String topic, Consumer<String> handler) and publish(String topic, String payload) (calls every handler of that topic in subscription order; unknown topics are ignored). Handlers are kept in a Map<String, List<Consumer<String>>> built with computeIfAbsent.
Read an integer n and n commands: sub <topic> <tag> registers a lambda that prints <tag>:<payload>; pub <topic> <payload> publishes. Also count all deliveries in a field and print deliveries=<count> at the end.
Example: 4 then sub news a, sub news b, pub news hello, pub sports x →
a:hello
b:hello
deliveries=2Memoised function factory
Write static <T, R> Function<T, R> memoize(Function<T, R> f) that returns a function which caches results in a HashMap<T, R> (using computeIfAbsent) and counts cache misses in a captured int[] exposed through a second static method — simplest: make memoize return a small class or record holding both the function and a misses() supplier. Then wrap a slow Function<Integer, Long> computing n * n (and incrementing nothing else).
Read an integer n and n integers; apply the memoised function to each and print the results space-separated, then misses=<distinct inputs>.
Example: 5 then 3 4 3 3 5 →
9 16 9 9 25
misses=3A command dispatcher
Build a Map<String, Consumer<String[]>> of commands, each a lambda operating on a shared List<String> items: add <x> appends, del <x> removes the first occurrence (printing missing <x> if absent), show prints the list, count prints count=<n>. An unknown command prints unknown <cmd>. Read n command lines and dispatch each by looking it up — no switch in the loop.
Example: 5 then add a, add b, del c, show, count →
missing c
[a, b]
count=2In this module: Nested classes & lambdas
← Functions as values — composition, factories and strategies · The exception hierarchy and how to read a stack trace →