Java SE 21 Developer Professional Free Sample Questions

Create a free account to browse all 17 sample questions. The full practice test includes 254 questions. Use the simulator for timed and flashcard mode.

Try Simulator

1Z0-830 Sample Questions

  1. Question 1

    Q1

    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?

    Show answer & explanation

    Correct answer: B

    Switch expressions must be exhaustive. Since Transaction is a sealed interface permitting CreditCard, BankTransfer, and DigitalWallet, all subtypes must be covered. The provided code handles CreditCard (both high value and standard) and BankTransfer, but it fails to handle DigitalWallet, causing a compilation error.

  2. Question 2

    Q2

    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?

    Show answer & explanation

    Correct answer: B

    In Java 21, using synchronized blocks or methods inside a virtual thread can cause the virtual thread to be 'pinned' to its carrier thread if it performs a blocking operation (like I/O) while holding the monitor. This prevents the carrier thread from executing other virtual threads. ReentrantLock does not suffer from this pinning behavior and allows the virtual thread to unmount properly during blocking wait states.

  3. Question 3

    Q3

    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?

    Show answer & explanation

    Correct answer: A

    Operations: 1. addFirst("Alpha") -> [Alpha]. 2. addLast("Omega") -> [Alpha, Omega]. 3. addFirst("Beta") -> [Beta, Alpha, Omega]. getFirst() returns "Beta", getLast() returns "Omega". reversed() returns a view in reverse order: [Omega, Alpha, Beta].

  4. Question 4

    Q4Multiple answers

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

    Show answer & explanation

    Correct answers: B, C

    Records are essentially final data carriers. They implicitly extend java.lang.Record, are final, and cannot be abstract. They also cannot extend other classes (single inheritance rule).

    While records cannot extend classes, they are fully capable of implementing one or more interfaces, just like normal classes.

  5. Question 5

    Q5

    You are implementing a module descriptor module-info.java for a library named com.example.lib. You need to ensure that the package com.example.lib.internal is accessible via reflection to a specific testing framework module com.test.framework, but not accessible to other modules at compile time or runtime.

    Which directive should you use?

    Show answer & explanation

    Correct answer: B

    The opens directive allows deep reflection (access to private members) on the package's types but does not make the package's types available for compile-time or static linkage. The qualified export (to com.test.framework) restricts this reflective access only to the specified module.

  6. Question 6

    Q6

    Consider the following Text Block usage in Java 21:

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

    If the opening """ is on line 1, and the closing """ is indented to align with the start of , how many spaces of indentation are removed from the tag line?

    Show answer & explanation

    Correct answer: B

    The compiler determines the 'common whitespace prefix' to strip based on the position of the closing delimiter and the non-empty lines. If the closing delimiter is aligned with , that level of indentation defines the baseline. Any indentation beyond that baseline (like the extra spaces for and ) is preserved.

  7. Question 7

    Q7

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

    Show answer & 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

    Q8

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

    Which code snippet correctly achieves this?

    Show answer & explanation

    Correct answer: A

    Duration.between(t1, t2) creates a Duration object representing the time elapsed. The toDays() method converts this duration to the number of whole 24-hour periods (days).

Register free to unlock 9 more sample questions

Create a free account to continue with the rest of the 1Z0-830 sample set.

Lifetime One

Own this practice test forever.

$
$79.99
one-time
  • Full access to 254 questions
  • Study, Timed & Flashcard Modes
  • All past and future versions i
  • Detailed Explanations
  • Study Tracking & Past Attempts
  • Brainy AI Assistant
  • Lifetime updates

Two

Any 2 exams per month.

$20.00/exam
$39.99
/month
  • 2 active exam slots
  • Study, Timed & Flashcard Modes
  • All past and future versions i
  • Detailed Explanations
  • Study Tracking & Past Attempts
  • 1,000 Brainy AI Credits
  • Cancel anytime

Premium Twelve

Any 12 exams over 3 months.

$15.00/exam
$179.99
/3 months
  • 4 active exam slots
  • Study, Timed & Flashcard Modes
  • All past and future versions i
  • Detailed Explanations
  • Study Tracking & Past Attempts
  • 15,000 Brainy AI Credits
  • Dedicated support
  • Friend seat included — full access

Trusted by professionals at

NvidiaSupabaseGitHubOpenAITursoClerkClaude AIAmazon