Java Refresh Test (Java 9 to 25)

Naveen AnamaneniUpdated · 8 min read

Coming back to a city you lived in years ago is a strange feeling. The streets are where you left them, but there is a new metro line, your old shortcut is now one-way, and everyone pays with their phone. Java after Java 8 feels the same. The language you knew still works, but there are records, var, text blocks, switch expressions, pattern matching, virtual threads and a new release every six months. This test is for the Refresh track. Every question is about a feature added in Java 9 or later.

How the Test Works

  • What's in it: 20 multiple-choice questions, then 3 output puzzles where you work out what a short program prints.
  • Time: 30 minutes, without notes, an IDE or JShell.
  • Pass mark: 16 out of 23. The quiz marks the 20 questions for you. Mark the puzzles yourself, one point each, and add them to your quiz score.
  • What it covers: language, library and JVM features from Java 9 to Java 25. Start with Java 9 to 11 Features if most of these are new to you.

Part 1: 20 Questions

Refresh test: 20 questions

20 questions
1What is the type of list in var list = new ArrayList<>();?
2How must a text block start?
3How does strip() (Java 11) differ from trim()?
4What is "ab".repeat(3)?
5Which is true of case X -> labels in a switch?
6What does stream.toList() (Java 16) return?
7What does List.copyOf(listWithNull) do if the list contains null?
8In Java 21, which call returns the last element of a List?
9Can a record declare extra instance fields?
10In a sealed hierarchy, what does non-sealed mean for a subclass?
11Which compiles?
12Where can the unnamed variable _ be used (Java 22 and later)?
13Which creates a virtual thread?
14What do scoped values (Java 25) mainly replace?
15Which package holds the modern HttpClient (Java 11)?
16What does import module java.base; (Java 25) do?
17In module-info.java, how does opens differ from exports?
18What can a Java 25 compact source file leave out?
19What extra information do helpful NullPointerException messages (Java 14 and later) give you?
20What are stream gatherers (Java 24) for?

Part 2: Output Puzzles

Work out what each program prints, on paper, before you open the solution. The solution shows the full program, its real output and a Try it button. Give yourself one point for each puzzle you get exactly right.

Puzzle 1: First, Last and Reversed

What is printed? (Java 21 or later)

Java
var list = new ArrayList<>(List.of(3, 1, 2));
list.addFirst(0);
System.out.println(list.reversed());
Show solution
SequencedPuzzle.java▶ Try it
import java.util.ArrayList;
import java.util.List;
 
public class SequencedPuzzle {
    public static void main(String[] args) {
        var list = new ArrayList<>(List.of(3, 1, 2));
        list.addFirst(0);
        System.out.println(list.reversed());
    }
}
Output
[2, 1, 3, 0]
Why this output?

addFirst(0) makes the list [0, 3, 1, 2]. reversed() returns a reversed view of it, without sorting anything. Revise: Sequenced Collections in Java.

Puzzle 2: A Guarded Case

What is printed?

Java
Object o = 42;
String r = switch (o) {
    case Integer i when i > 40 -> "big int " + i;
    case Integer i -> "int " + i;
    case String s -> "string " + s;
    default -> "other";
};
System.out.println(r);
Show solution
GuardPuzzle.java▶ Try it
public class GuardPuzzle {
    public static void main(String[] args) {
        Object o = 42;
        String r = switch (o) {
            case Integer i when i > 40 -> "big int " + i;
            case Integer i -> "int " + i;
            case String s -> "string " + s;
            default -> "other";
        };
        System.out.println(r);
    }
}
Output
big int 42
Why this output?

The cases are tried from top to bottom, and the first match wins. 42 is an Integer and the guard i > 40 is true, so the first case matches. Revise: Guards in Switch (When Clause).

Puzzle 3: Counting Lines in a Text Block

What is printed?

Java
String json = """
    {
      "name": "Java"
    }
    """;
System.out.println(json.lines().count());
Show solution
TextBlockPuzzle.java▶ Try it
public class TextBlockPuzzle {
    public static void main(String[] args) {
        String json = """
            {
              "name": "Java"
            }
            """;
        System.out.println(json.lines().count());
    }
}
Output
3
Why this output?

The line with the opening """ isn't part of the text, and the common indentation is removed. That leaves three lines: {, "name": "Java" and }. The closing """ on its own line adds a final line break, but not a fourth line. Revise: Text Blocks in Java.

What Your Score Means

Score (out of 23)What it meansWhat to do next
20 to 23You are up to date with modern JavaTry the Java Deep Dive Test, or move on to Core Java Interview Questions
16 to 19A pass, with a few gapsRead the lessons linked from the questions you missed
Under 16Not yetWork through Java 9 to 11 Features and the lessons that follow it, then take the test again

Was this tutorial helpful?