**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?
Q2
**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?
Q3
A developer is building a system that processes different geometric shapes. The following `sealed` interface is defined: ```java 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: ```java // in another package public final class Triangle implements Shape { /* ... */ } ``` What is the result of this attempt?
Q4
**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? ```mermaid classDiagram direction LR class Account { > +getAccountNumber() +getBalance() } class CheckingAccount class SavingsAccount class InvestmentAccount Account <|.. CheckingAccount : implements Account <|.. SavingsAccount : implements Account <|.. InvestmentAccount : implements ```
Q5
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: ```java 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 -> { if (d.amount() > 10000) yield 2; yield 1; } case Withdrawal w -> { if (w.amount() > 5000) yield 3; yield 1; } // Missing Transfer case }; } } ``` What is the result of compiling the `TransactionProcessor` class?
Q6
A developer is designing a system to model hierarchical configurations. A `ConfigNode` can contain other `ConfigNode` objects. The design must ensure that only specific, predefined types of nodes (`FileNode`, `DirectoryNode`, `PropertyNode`) can extend `ConfigNode`. Additionally, `DirectoryNode` itself must be extensible, but only by `RootDirectoryNode`. Which class and interface structure best achieves these requirements?
Q7
A developer is modeling a system for handling financial transactions using a sealed interface. The goal is to ensure that all possible transaction types are handled in a `switch` expression without requiring a `default` case. Given the following sealed hierarchy, what is the problem with the `processTransaction` method? ```java sealed interface Transaction permits Deposit, Withdrawal, FxTrade {} final record Deposit(double amount) implements Transaction {} final record Withdrawal(double amount) implements Transaction {} // FxTrade is a non-sealed class to allow for future extension non-sealed class FxTrade implements Transaction {} class SpotTrade extends FxTrade {} public class TransactionProcessor { public static String processTransaction(Transaction tx) { return switch (tx) { case Deposit d -> "Processing Deposit"; case Withdrawal w -> "Processing Withdrawal"; case FxTrade f -> "Processing FX Trade"; }; } } ```
Q8
**Case Study** A development team at a logistics company is modeling various types of shipping containers. The requirements are as follows: 1. There are three primary types of containers: `Standard`, `Refrigerated`, and `OpenTop`. 2. All container types must have a `capacity` and a `containerId`. 3. `Refrigerated` containers must also have a `temperature` setting. 4. The system must be able to handle these specific container types exhaustively in `switch` expressions for processing logic. 5. In the future, a subcontractor may need to introduce a new `HeavyDuty` container type that extends the `Standard` container's functionality. This new type should NOT break the exhaustiveness of existing `switch` statements. Which class hierarchy design best fulfills all these requirements using modern Java features? ```mermaid classDiagram direction LR class ShipmentProcessor { +process(Container c) } class Container { > +double capacity +String containerId } class Standard { +double capacity +String containerId } class Refrigerated { +double capacity +String containerId +double temperature } class OpenTop { +double capacity +String containerId } class HeavyDuty { +double maxLoad } Container <|.. Standard Container <|.. Refrigerated Container <|.. OpenTop Standard <|-- HeavyDuty ShipmentProcessor ..> Container : processes ```
Q9
A developer is implementing a `sealed` interface to model different types of application events. A `switch` expression is used to process these events. What is the result of compiling and running the following code? ```java sealed interface AppEvent permits LoginEvent, LogoutEvent, ErrorEvent { String getUserId(); } final record LoginEvent(String userId, String ipAddress) implements AppEvent {} final record LogoutEvent(String userId, String reason) implements AppEvent {} final record ErrorEvent(String userId, String errorMsg) implements AppEvent {} public class EventProcessor { public String process(AppEvent event) { return switch (event) { case LoginEvent le -> "Login from " + le.ipAddress(); case LogoutEvent le -> "Logout because " + le.reason(); // Missing case for ErrorEvent }; } } ``` ```mermaid graph TD A[AppEvent] --> B(LoginEvent) A --> C(LogoutEvent) A --> D(ErrorEvent) style A fill:#f9f,stroke:#333,stroke-width:2px ```
Q10
A financial application needs to process transactions from different time zones. A developer writes the following code to handle a transaction timestamp from Tokyo and convert it to the system's default time zone. What is the outcome of executing this code snippet?