Java Interfaces Quiz: Default Methods and Sealed Types Test
Test your Java interface skills with 12 questions and two programs on default and static methods, functional interfaces, Comparable, Iterable and sealed types.
- Course: Java study plan
- Module: Interfaces
- Kind: Checkpoint — cleared at 70%
- Reading time: 22 min
- Runtime: OpenJDK 18
Checkpoint — Interfaces is the checkpoint that closes the Interfaces module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers interface basics, default/static/private methods and conflict resolution, functional interfaces, Comparable and Iterable, and sealed interfaces.
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 interface may contain, and why implementing methods must be
public. - The three rules for resolving two defaults with the same signature.
- What makes an interface functional, and what
@FunctionalInterfaceenforces. - Why
compareToshould agree withequals, and which collections rely on it. - What
Iterablerequires, and whatnext()must do past the end. - What
sealed … permitsbuys, and the modifiers a permitted type needs.
The programs are an Iterable type with a natural order, and a strategy-style interface implemented by several small classes and a lambda.
Common questions
What are the three rules for resolving two default methods with the same signature?
First, a method defined in the class or a superclass wins over any interface default. Second, a more specific interface wins over one it extends. Otherwise the class must override the method itself, and may call a chosen default with Swimmer.super.move().
What must next() do when an iterator has no more elements?
Throw NoSuchElementException. A call to next() without a preceding successful hasNext() must never make up a value, and each call to iterator() should return a fresh, independent iterator.
What makes an interface functional in Java?
Having exactly one abstract method; defaults, statics and redeclared Object methods do not count. @FunctionalInterface makes the compiler enforce that, so a second abstract method becomes a compile error rather than breaking the lambdas that implement the interface.
Exercises
Ordered, iterable playlist
Write final class Track implements Comparable<Track> with a title and a duration in seconds; natural order is by duration ascending, then title. Write class Playlist implements Iterable<Track> that stores tracks in insertion order and offers sorted() returning a new List<Track> in natural order (Collections.sort on a copy).
Read an integer n and n lines <title> <seconds> (titles have no spaces). Print the playlist in insertion order via for-each, one per line as <title> (<seconds>s), then a line --, then the sorted order in the same format.
Example: 3 then intro 30, epic 300, bridge 30 →
intro (30s)
epic (300s)
bridge (30s)
--
bridge (30s)
intro (30s)
epic (300s)Pricing strategies
Declare @FunctionalInterface interface Pricing { long price(long baseCents, int qty); }. Provide three implementations: a class Flat (base × qty), a class Bulk (10% off the flat price when qty ≥ 10, integer division), and a lambda for Member pricing (flat price minus 200 cents per item, never below 0). Read an integer n and n lines <strategy> <baseCents> <qty> where strategy is flat, bulk or member; look up the Pricing in a Map<String, Pricing> and print the price.
Example: 3 then flat 1000 2, bulk 1000 10, member 100 3 →
2000
9000
0Listeners you can unsubscribe
Declare @FunctionalInterface interface Listener { void on(String event); } and class Bus with Runnable subscribe(Listener l) — it registers the listener and returns a Runnable that removes it again — and void publish(String event) which calls every current listener in subscription order. Read n commands: sub <name> subscribes a listener that prints <name> got <event>; unsub <name> runs that listener's returned Runnable; pub <event> publishes. Print listeners=<count> at the end.
Example: 5 then sub a, sub b, pub hello, unsub a, pub bye →
a got hello
b got hello
b got bye
listeners=1In this module: Interfaces
← Sealed interfaces — closed sets of types · Enums — a fixed set of named instances →