AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Skip to content

19 Modern Java / Checkpoint

Checkpoint — Modern Java

26 min · +25 XP · 3 exercises · 15 questions

Exercises0/3
Quiz · 70% to clear15 q

This checkpoint covers the release cadence and the LTS milestones, var and inference, pattern instanceof, pattern switch and record patterns with exhaustiveness, the library additions from 9 to 21, virtual threads and structured concurrency, and the module system with the modern toolchain.

How it works. Fifteen questions and three programs; 70% on the questions and every program accepted clears the module.

Before you start, make sure you can answer:

  • Which versions are LTS, and which of records, sealed, var, text blocks, pattern switch and virtual threads arrived in each.
  • Where var is not allowed, and what var list = new ArrayList<>() infers.
  • How a pattern variable is scoped, and what sealed buys an exhaustive switch.
  • List.of versus Arrays.asList versus Collections.unmodifiableList; strip versus trim.
  • What a virtual thread is, when it helps, and what pinning means.
  • requires transitive, opens, and what the unnamed module is.

The programs — which compile on the Java 17-level runtime — are a shape calculator over a sealed hierarchy with pattern instanceof, a modern text pipeline using the new String and Stream methods, and an immutable configuration built from the collection factories.

Common questions

In which Java versions did var, records, sealed classes and virtual threads arrive?

var arrived in Java 10, text blocks were final in 15, records in 16, sealed classes in 17, and pattern matching for switch and virtual threads in 21. The LTS versions are 8, 11, 17, 21 and 25.

What does a sealed type buy an exhaustive switch?

Because a sealed type lists every permitted subtype, the compiler can prove a switch covers them all without a default, and adding a subtype makes every switch that misses it fail to compile.

How is a pattern variable scoped in Java?

A pattern variable such as s in o instanceof String s exists only where the match is certain: inside the if body, on the right of &&, and after an early return on the negated test — never on the right of ||.

Modern Java Quiz: var, Pattern Matching and Java 21 Practice

Test modern Java with 15 questions and three programs on LTS versions, var, pattern matching, sealed types, new APIs, virtual threads and the module system.

  • Course: Java study plan
  • Module: Modern Java
  • Kind: Checkpoint — cleared at 70%
  • Reading time: 26 min
  • Runtime: OpenJDK 18

Checkpoint — Modern Java is the checkpoint that closes the Modern Java module: a graded quiz and whole-program exercises, passed at 70%.

Instructions

This checkpoint covers the release cadence and the LTS milestones, var and inference, pattern instanceof, pattern switch and record patterns with exhaustiveness, the library additions from 9 to 21, virtual threads and structured concurrency, and the module system with the modern toolchain.

How it works. Fifteen questions and three programs; 70% on the questions and every program accepted clears the module.

Before you start, make sure you can answer:

  • Which versions are LTS, and which of records, sealed, var, text blocks, pattern switch and virtual threads arrived in each.
  • Where var is not allowed, and what var list = new ArrayList<>() infers.
  • How a pattern variable is scoped, and what sealed buys an exhaustive switch.
  • List.of versus Arrays.asList versus Collections.unmodifiableList; strip versus trim.
  • What a virtual thread is, when it helps, and what pinning means.
  • requires transitive, opens, and what the unnamed module is.

The programs — which compile on the Java 17-level runtime — are a shape calculator over a sealed hierarchy with pattern instanceof, a modern text pipeline using the new String and Stream methods, and an immutable configuration built from the collection factories.

Common questions

In which Java versions did var, records, sealed classes and virtual threads arrive?

var arrived in Java 10, text blocks were final in 15, records in 16, sealed classes in 17, and pattern matching for switch and virtual threads in 21. The LTS versions are 8, 11, 17, 21 and 25.

What does a sealed type buy an exhaustive switch?

Because a sealed type lists every permitted subtype, the compiler can prove a switch covers them all without a default, and adding a subtype makes every switch that misses it fail to compile.

How is a pattern variable scoped in Java?

A pattern variable such as s in o instanceof String s exists only where the match is certain: inside the if body, on the right of &&, and after an early return on the negated test — never on the right of ||.

Exercises

Shape report

Declare sealed interface Shape permits Circle, Square, Rect with records Circle(double r), Square(double side), Rect(double w, double h). Read an integer n and n shapes (circle r, square s, rect w h). Using pattern instanceof for the area, print for every kind present, in alphabetical order, <Kind>: n=<count> area=<total area, 2 dp>; then largest=<record toString of the largest shape> and kinds=<number of distinct kinds>. Use Locale.ROOT.

Example: 3 then circle 1, rect 2 3, circle 2 →

Circle: n=2 area=15.71
Rect: n=1 area=6.00
largest=Circle[r=2.0]
kinds=2

Word bars

Read all of standard input. Build one modern pipeline: lines() → strip → drop blank lines with Predicate.not(String::isBlank) → split into words → lower-case → count with groupingBy(…, counting()). Print the top 5 words (most frequent first, ties alphabetical) as "%-8s %s %d".formatted(word, "#".repeat(count), count), then distinct=<distinct words>.

Example: the cat the dog / The end →

the      ### 3
cat      # 1
dog      # 1
end      # 1
distinct=4

Immutable configuration

Read key=value lines until end of input (skip blanks) into a mutable map, then freeze it with Map.copyOf. Print port=<value or 8080>, host=<value or localhost> and debug=<value or false> using Objects.requireNonNullElse for the defaults; then keys=<all keys, sorted, comma-separated>; then try config.put("x", "y") and print frozen=true if it throws UnsupportedOperationException.

Example: port=9090 / debug=true →

port=9090
host=localhost
debug=true
keys=debug,port
frozen=true

In this module: Modern Java

← Modules, jshell and the modern toolchain · The interview template — input, output, limits →