Java SE 21 Developer Professional Free Sample Questions

Description

Covers primitives, text and date-time handling, program flow, object lifecycle, inheritance and polymorphism, exceptions, collections, streams and lambdas, the module system, and concurrency.

17 free sample questions253 in the full practice test

Try simulator

1Z0-830 Sample Questions

  1. Question 1

    Intermediate

    Controlling Program Flow · Create program flow control constructs including if/else, switch statements and expressions, loops, and break and continue statements.

    A financial application uses a sealed interface Transaction to represent various payment methods. The interface permits three record classes: CreditCard, BankTransfer, and DigitalWallet. You are writing a method to process these transactions using a switch expression with pattern matching.

    public sealed interface Transaction permits CreditCard, BankTransfer, DigitalWallet {}
    public record CreditCard(String pan, double amount) implements Transaction {}
    public record BankTransfer(String iban, double amount) implements Transaction {}
    public record DigitalWallet(String id, double amount) implements Transaction {}
    
    // In a service class:
    public String process(Transaction t) {
    return switch(t) {
    case CreditCard c when c.amount() > 1000 -> "High Value CC";
    case CreditCard c -> "Standard CC";
    case BankTransfer b -> "Bank Transfer";
    // Missing case
    };
    }
    

    Which statement is true regarding the compilation of this code?

    Answer and explanation

    Correct answer: D

    A switch expression over a sealed interface must cover every permitted subtype or have a default. No case handles DigitalWallet, so the compiler rejects the switch. The when guard is a standard Java 21 feature.

  2. Question 2

    Advanced

    Managing Concurrent Code Execution · Develop thread-safe code, using locking mechanisms and concurrent API.

    You are developing a high-throughput web server using Java 21's Virtual Threads. You need to protect a shared resource using a locking mechanism. However, you want to avoid 'pinning' the virtual thread to the carrier thread, which can degrade performance during blocking operations.

    Which locking approach should you prefer?

    Answer and explanation

    Correct answer: A

    In Java 21, a virtual thread in a synchronized block is pinned to its carrier. If it blocks there, the carrier blocks too. With ReentrantLock, the virtual thread unmounts while it waits, and the carrier runs other virtual threads.

  3. Question 3

    Beginner

    Working with Arrays and Collections · Create arrays, List, Set, Map and Deque collections, and add, remove, update, retrieve and sort their elements.

    Review the following code snippet that uses the new Sequenced Collections API introduced in Java 21:

    import java.util.*;
    
    public class SequenceTest {
    public static void main(String[] args) {
    SequencedCollection sc = new ArrayList ();
    sc.addFirst("Alpha");
    sc.addLast("Omega");
    sc.addFirst("Beta");
    
    System.out.println(sc.getFirst() + "-" + sc.getLast());
    System.out.println(sc.reversed());
    }
    }
    

    What is the output of this program?

    Answer and explanation

    Correct answer: D

    ArrayList is a List, and List is a SequencedCollection in Java 21. The calls build [Beta, Alpha, Omega]. So getFirst is Beta and getLast is Omega. reversed() returns a reverse-order view, which prints [Omega, Alpha, Beta].

  4. Question 4

    IntermediateMultiple answers

    Using Object-Oriented Concepts in Java · Create classes and records, and define and use instance and static fields and methods, constructors, and instance and static initializers.

    Which of the following are valid characteristics of Java records? (Select TWO)

    Answer and explanation

    Correct answers: A, D

    A record is implicitly final, so no class can extend it. A record also cannot be declared abstract.

    A record cannot extend a class, because it already extends java.lang.Record. It can still implement any number of interfaces, just like a normal class.

  5. Question 5

    Intermediate

    Packaging and Deploying Java Code · Define modules and expose module content, including that by reflection, and declare module dependencies, define services, providers, and consumers.

    You are implementing a module descriptor module-info.java for a library named com.example.lib. A testing framework module, com.test.framework, must be able to use reflection on all members of the classes in the package com.example.lib.internal, including private ones. No module, including the framework, may compile against that package.

    Which directive should you use?

    Answer and explanation

    Correct answer: B

    opens ... to com.test.framework gives only that module reflective access at run time, including private members. It does not export the package, so no module can compile against it.

  6. Question 6

    Intermediate

    Handling Date, Time, Text, Numeric and Boolean Values · Manipulate text, including text blocks, using String and StringBuilder classes.

    Consider the following Text Block usage in Java 21:

    String html = """
    
    Hello, World
    
    """;
    

    The closing """ is on its own line, 4 spaces to the left of . How many spaces of indentation are removed from the tag line?

    Answer and explanation

    Correct answer: A

    The compiler removes the smallest indentation among the content lines and the closing delimiter line. The closing delimiter is the least indented here. So its indentation is removed from every line, and keeps 4 spaces.

  7. Question 7

    Beginner

    Managing Concurrent Code Execution · Create both platform and virtual threads. Use both Runnable and Callable objects, manage the thread lifecycle, and use different Executor services and concurrent API to run tasks.

    True or False: In Java 21, creating a Virtual Thread requires implementing a specific sub-interface of Runnable.

    Answer and explanation

    Correct answer: B

    Virtual threads run standard java.lang.Runnable tasks. There is no special interface required. You can create them using Thread.ofVirtual().start(runnable).

  8. Question 8

    Intermediate

    Handling Date, Time, Text, Numeric and Boolean Values · Manipulate date, time, duration, period, instant and time-zone objects including daylight saving time using Date-Time API.

    You need to perform a calculation using java.time.Duration. You have a start time t1 and end time t2 (both Instant, with t2 later than t1). You want to calculate the number of whole days between them.

    Which code snippet correctly achieves this?

    Answer and explanation

    Correct answer: C

    Duration.between(t1, t2) holds the exact time from t1 to t2. toDays() returns the number of whole 24-hour days in it. Swapped arguments give a negative value, and Period works only with dates.

Register free for 9 more questions

Or unlock all 253 1Z0-830 questions with explanations, timed mode and flashcards.