Skip to content

Java SE 17 Developer (OCP) Practice Questions

Prepare for 1Z0-829 with more than an answer.

225 questions in the full set20 sample questionsUpdated Jan 23, 2026
Exam fee
$245 USD
Level
Professional
Valid for
Lifetime (no expiration)
Domains covered on the exam 11
  1. Handling date, time, text, numeric and boolean values10%
  2. Controlling Program Flow8%
  3. Utilizing Java Object-Oriented Approach30%
  4. Handling Exceptions7%
  5. Working with Arrays and Collections10%
  6. Working with Streams and Lambda expressions12%
  7. Packaging and deploying Java code and use the Java Platform Module System8%
  8. Managing concurrent code execution8%
  9. Using Java I/O API7%
  10. Accessing databases using JDBC5%
  11. Implementing Localization5%
  1. 1

    A developer is writing a JDBC utility to fetch user data. They need to ensure that the Connection and PreparedStatement are always closed, even if an exception occurs during result set processing. Which code structure provides the most robust and modern approach to achieve this?

    Show answer details

    Correct answer: C

    The try-with-resources statement is the modern, preferred way to manage resources that implement the AutoCloseable interface, such as Connection, Statement, and ResultSet. By declaring the resources in the parentheses after try, the developer guarantees that close() will be called on them automatically at the end of the block, in the correct reverse order of declaration, regardless of whether an exception is thrown. This approach is more concise and less error-prone than manual finally blocks.

  2. 2

    True or False: In the Java Platform Module System (JPMS), if module A requires module B (requires B;), and module B exports package p1 (exports p1;), then module A can access public types in package p1 by default.

    Show answer details

    Correct answer: A

    This statement is true. This describes the fundamental principle of readability in JPMS. The requires directive in module A makes module B readable to A. The exports directive in module B makes the public types within package p1 accessible to any module that requires B. Therefore, module A can access the public types in p1.

  3. 3

    A developer is working with a java.util.concurrent.ExecutorService to run asynchronous tasks. After submitting all tasks, the developer wants to shut down the service gracefully, allowing currently executing tasks to finish but rejecting any new tasks. Which method should be called on the ExecutorService instance?

    Show answer details

    Correct answer: A

    The shutdown() method initiates an orderly shutdown of the ExecutorService. It stops accepting new tasks, but previously submitted tasks will be allowed to complete. This is the standard method for a graceful shutdown.

  4. 4

    Consider the following record definition and its usage. What will be the output of this program?

    Show answer details

    Correct answer: C

    The code defines a record Point with a compact constructor for validation. Records automatically generate accessor methods (e.g., x(), y()), and an equals() method that compares the state of the record components. Since p1 and p2 are created with the same x and y values, p1.equals(p2) will return true. The first print statement correctly accesses the components.

  5. 5

    A developer is serializing an object of class Employee. The Employee class contains a field for a password, which should not be saved to disk for security reasons. The class also needs to maintain a version UID for compatibility. Which two modifications are necessary to achieve this? (Select TWO)

    Show answer details

    Correct answer: A, C

    For an object to be serializable, its class must implement the java.io.Serializable marker interface. This is the primary requirement to enable the serialization mechanism for a class.

    The transient keyword is used to mark a field that should be excluded from the serialization process. When an object is serialized, the values of its transient fields are ignored.

  6. 6

    Case Study:

    A logistics company is modernizing its package tracking system. The system needs to represent different types of packages, each with specific attributes. The requirements are as follows:

    1. There are three and only three types of packages: Standard, Express, and Fragile.
    2. No other package types should be permissible in the system to ensure strict domain modeling.
    3. A Standard package has a weight.
    4. An Express package has a weight and a priority level (an integer).
    5. A Fragile package has a weight and special handling instructions (a String).
    6. All package types should be immutable data carriers.

    A senior developer proposes using a combination of sealed interfaces and records to model this domain. The goal is to create a type-safe hierarchy that can be effectively processed, for example, in a switch expression for calculating shipping costs.

    Which code implementation best satisfies all the given requirements?

    Show answer details

    Correct answer: B

    This solution perfectly meets all requirements. The sealed interface Package with a permits clause ensures that only Standard, Express, and Fragile can implement it, satisfying requirement #1 and #2. Using record for each package type makes them immutable data carriers with appropriate fields, satisfying requirements #3, #4, #5, and #6. This structure is ideal for modern, type-safe domain modeling and works perfectly with pattern matching in switch expressions.

  7. 7

    Case Study

    A software architect is designing a system for a payment gateway. The system must handle different types of payment methods. The core requirement is to have a common PaymentMethod type, but restrict implementations to a known, fixed set: CreditCard, PayPal, and BankTransfer. No other developers, now or in the future, should be able to create new, unknown implementations of PaymentMethod outside of the core payment module. This strict control is necessary for security and compliance audits.

    The architect wants to model this domain using the most effective and modern object-oriented features available in Java 17 to enforce this restriction at the compiler level.

    Which design provides the strongest compile-time safety and best expresses the architect's intent?

    Show answer details

    Correct answer: D

    This is the ideal solution. The sealed keyword is designed precisely for this scenario. It allows a class or interface to declare which other types are permitted to extend or implement it. This enforces the restriction at compile time, perfectly matching the architect's requirement for a closed set of implementations.

  8. 8

    A developer is building a system that processes different geometric shapes. The following sealed interface is defined:

    sealed interface Shape permits Circle, Rectangle {}
    final class Circle implements Shape { /* ... */ }
    final class Rectangle implements Shape { /* ... */ }
    

    Later, another developer in a different package tries to define a new shape:

    // in another package
    public final class Triangle implements Shape { /* ... */ }
    

    What is the result of this attempt?

    Show answer details

    Correct answer: C

    The purpose of a sealed interface or class is to restrict which other types can implement or extend it. Because Shape is sealed and only permits Circle and Rectangle, any attempt to implement it with another class like Triangle will result in a compilation error. This is a key feature of sealed types for creating closed hierarchies.

  9. 9

    Case Study

    A financial services company is designing a system to model various types of accounts. The system must support CheckingAccount, SavingsAccount, and InvestmentAccount. A key architectural requirement is that the set of account types is fixed and cannot be extended by other development teams or third-party libraries. This ensures that all account processing logic can be handled exhaustively and safely within the core application.

    The architect wants to create a common supertype called Account that all specific account types will implement. The solution must enforce the closed hierarchy at compile time and should use modern Java features for conciseness and type safety.

    Which code snippet best models this domain according to the requirements?

    classDiagram direction LR class Account { > +getAccountNumber() +getBalance() } class CheckingAccount class SavingsAccount class InvestmentAccount Account <|.. CheckingAccount : implements Account <|.. SavingsAccount : implements Account <|.. InvestmentAccount : implements
    Show answer details

    Correct answer: B

    This solution perfectly meets the requirements. Using a sealed interface with a permits clause restricts the implementation of Account to only the three specified classes (CheckingAccount, SavingsAccount, InvestmentAccount). This enforces the closed hierarchy at compile time, preventing any other class from implementing the Account interface. Using final record for the implementations is a good choice for concise, immutable data carriers, which is often suitable for account types.

  10. 10

    A financial data processing application uses a sealed interface Transaction with three implementing records: Deposit, Withdrawal, and Transfer. A method is designed to process a list of these transactions and calculate a risk score. Examine the following code:

    sealed interface Transaction permits Deposit, Withdrawal, Transfer {}
    record Deposit(double amount) implements Transaction {}
    record Withdrawal(double amount) implements Transaction {}
    record Transfer(double amount, String recipient) implements Transaction {}
    
    public class TransactionProcessor {
    public int calculateRisk(Transaction tx) {
    return switch (tx) {
    case Deposit d -&gt; {
    if (d.amount() &gt; 10000) yield 2;
    yield 1;
    }
    case Withdrawal w -&gt; {
    if (w.amount() &gt; 5000) yield 3;
    yield 1;
    }
    // Missing Transfer case
    };
    }
    }
    

    What is the result of compiling the TransactionProcessor class?

    Show answer details

    Correct answer: B

    The compiler enforces exhaustiveness for switch expressions on sealed types. Since Transaction is a sealed interface permitting Deposit, Withdrawal, and Transfer, any switch expression on a Transaction object must handle all three cases or provide a default case. The provided code is missing a case for Transfer, so it will result in a compilation error.

Create an account to continue.