AlgoMaster Logo

Modern Java Features - Quiz

Last Updated: December 6, 2025

1 min read

Modern Java Features Exercises

31 quizzes

1
Code Completion

Infer the variable type while creating a list of active user names

java
1
activeNames = List.of("Alice", "Bob");

Click an option to fill the blank:

2
Code Completion

Use a switch expression to map a status code to a human-readable label

java
1
String label = switch (status) { case 200 -> "OK"; case 404 -> "Not Found"; default -> ; };

Click an option to fill the blank:

3
Code Completion

Match a String input and bind it to a variable only when it is a String

java
1
if (input instanceof String ) System.out.println(s.length());

Click an option to fill the blank:

4
Multiple Choice

Where can the var keyword be used in Java 10 and later?

5
Multiple Choice

What is a key advantage of text blocks (""" ... """) in Java?

6
Multiple Choice

Compared to traditional switch statements, what do switch expressions add?

7
Multiple Choice

What does pattern matching for instanceof primarily remove from typical code?

8
Multiple Choice

In pattern matching for switch, what happens if a sealed hierarchy is used and a subclass is not handled?

9
Multiple Choice

Which statement about records in Java is true?

10
Multiple Choice

What do sealed classes and interfaces allow you to control?

11
Multiple Choice

What is the main purpose of unnamed patterns in Java 22?

12
Multiple Choice

Which interface introduced in Java 21 ensures a defined order for elements?

13
Multiple Choice

Which benefit do String Templates in Java 21 provide?

14
Multiple Choice

Why is using var with complex generic types often helpful?

15
Sequencing

Order the steps to define and use a record to hold a user's name and age, then print it.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to use a sealed interface with pattern matching for switch to process shapes.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code using var and text blocks?

1var sql = """
2SELECT id, name
3FROM users
4WHERE active = true
5""";
6System.out.println(sql.lines().count());
18
Output Prediction

What is the output of this switch expression?

1int day = 6;
2String type = switch (day) {
3    case 6, 7 -> "WEEKEND";
4    default -> "WEEKDAY";
5};
6System.out.println(type);
19
Output Prediction

What is the output when using pattern matching for instanceof?

1Object value = 42;
2String result;
3if (value instanceof Integer i && i > 40) {
4    result = "large";
5} else {
6    result = "small";
7}
8System.out.println(result);
20
Output Prediction

What is the output when using a sequenced collection?

1java.util.SequencedCollection<String> sc = new java.util.LinkedList<>();
2sc.addLast("A");
3sc.addFirst("B");
4System.out.println(sc.getFirst() + sc.getLast());
21
Output Prediction

What is the output when using a simple string template?

1int count = 3;
2String message = `Items: ${count + 2}`;
3System.out.println(message);
22
Bug Spotting

Find the bug when using var and text blocks to build a JSON string.

Click on the line(s) that contain the bug.

java
1
public class JsonBuilder {
2
    public static void main(String[] args) {
3
        var json = """
4
        {"name": "Alice"}
5
        """;
6
        System.out.println(json.trim());
7
    }
8
}
23
Bug Spotting

Find the bug in this switch expression using pattern matching for a sealed hierarchy.

Click on the line(s) that contain the bug.

java
1
sealed interface Command permits Start, Stop {}
2
final class Start implements Command {}
3
final class Stop implements Command {}
4
 
5
public class Runner {
6
    public static String handle(Command cmd) {
7
        return switch (cmd) {
8
            case Start s -> "start";
9
            case Stop s -> "stop";
10
        };
11
    }
12
}
24
Matching

Match each modern Java feature with its main purpose.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each pattern-related feature with how it is used.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code to define an immutable configuration record and print one of its components.

java
1
public record AppConfig(String name, int port) {}
2
3
public class Main {
4
public static void main(String[] args) {
5
AppConfig config = new AppConfig("service", 8080);
6
System.out.println(config.());
7
}
8
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to use a sequenced collection to add elements at both ends.

java
1
import java.util.LinkedList;
2
import java.util.SequencedCollection;
3
4
public class Demo {
5
public static void main(String[] args) {
6
SequencedCollection<String> items = new LinkedList<>();
7
items.("first");
8
items.("last");
9
System.out.println(items);
10
}
11
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to use pattern matching for switch on a sealed hierarchy.

java
1
sealed interface Shape permits Circle, Rectangle {}
2
record Circle(double radius) implements Shape {}
3
record Rectangle(double width, double height) implements Shape {}
4
5
public class Main {
6
public static String describe(Shape shape) {
7
return switch (shape) {
8
case Circle c -> "Circle: " + c.radius();
9
case Rectangle r -> + r.width();
10
};
11
}
12
}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code to define a sealed interface and a final permitted implementation.

java
1
public sealed interface Response permits {}
2
3
public final class implements Response {
4
private final int code;
5
public (int code) {
6
this.code = code;
7
}
8
public int code() {
9
return code;
10
}
11
}

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that performs pattern matching for instanceof and binds the variable.

Click on the line to select.

java
1
public class Demo {
2
    public static void main(String[] args) {
3
        Object obj = "hello";
4
        if (obj instanceof String s) {
5
            System.out.println(s.toUpperCase());
6
        }
7
    }
8
}
31
Hotspot Selection

Click the line that uses a switch expression with pattern matching over a sealed type.

Click on the line to select.

java
1
sealed interface Event permits Login, Logout {}
2
record Login(String user) implements Event {}
3
record Logout(String user) implements Event {}
4
 
5
public class Handler {
6
    public static String handle(Event e) {
7
        return switch (e) {
8
            case Login l -> "in:" + l.user();
9
            case Logout o -> "out:" + o.user();
10
        };
11
    }
12
}

Premium Content

Subscribe to unlock full access to this content and more premium articles.