1Z0-819 Sample Questions

1Z0-819 Sample Questions & Answers

Tests your grasp of the Java Platform Module System, concurrency, JDBC, localization and annotations, plus handling primitives and text values, program flow, the object lifecycle, exceptions, arrays, lambda expressions and streams, and generics with collections.

Launch the full 1Z0-819 simulator →

Showing 10 of 20 free samples.

  1. Question 1Beginner

    Localization · Parse and format messages, dates, and numbers

    A developer is writing a utility method to format currency values for different regions. The method should take a double and a java.util.Locale and return a formatted string. The application will be used in the US, Germany, and Japan. Which is the correct way to instantiate and use a NumberFormat object for this purpose?

    Show answer & explanation

    Correct answer: B

    NumberFormat is an abstract class and cannot be instantiated directly with new. The correct way to obtain a locale-specific formatter, especially for currency, is by using the static factory method NumberFormat.getCurrencyInstance(locale). This ensures that the correct currency symbol, grouping separators, and decimal separators are used according to the rules of the specified Locale.

  2. Question 2Intermediate

    Java Object-Oriented Approach · Utilize polymorphism and casting to call methods

    Given the following code:

    public class Device {
    private String type = "electronic";
    public String getType() { return type; }
    }
    
    public class Phone extends Device {
    private String type = "mobile";
    @Override
    public String getType() { return type; }
    }
    
    public class Tester {
    public static void main(String[] args) {
    Device device = new Phone();
    System.out.println(device.type + ":" + device.getType());
    }
    }
    

    What is the result?

    Show answer & explanation

    Correct answer: B

    This question tests the difference between method overriding (dynamic dispatch) and field hiding (static dispatch). When device.getType() is called, polymorphism ensures the overridden method in the Phone class is executed, returning "mobile". However, when device.type is accessed, the reference type (Device) determines which field is used, not the object type (Phone). Therefore, the type field from the Device class is accessed, which is "electronic".

  3. Question 3Intermediate

    Working with Streams and Lambda expressions · Perform decomposition and reduction

    A developer needs to process a list of Order objects. The goal is to create a Map where keys are the Customer associated with the order, and values are a List of all Order objects for that customer. Which stream operation correctly achieves this?

    graph TD A[List ] --> B{Stream Pipeline} B --> C[Map >]
    Show answer & explanation

    Correct answer: C

    The Collectors.groupingBy() collector is specifically designed for this purpose. It takes a classification function (in this case, Order::getCustomer) and groups the elements of the stream into a Map. The keys of the map are the results of applying the classification function, and the values are Lists of the stream elements that produced each key.

  4. Question 4IntermediateSelect 2

    Annotations · Create, apply, and process annotations

    A developer has created a custom annotation @Version to track the version of a class. The annotation must be available at runtime via reflection and should be included in the generated Javadoc.

    // line n1
    public @interface Version {
    String value();
    }
    

    Which meta-annotations should be placed at line n1 to meet these requirements? (Select TWO)

    Show answer & explanation

    Correct answers: A, D

    To make an annotation available at runtime through reflection, its retention policy must be set to RetentionPolicy.RUNTIME. To ensure the annotation is included in the output of documentation tools like Javadoc, the @Documented meta-annotation must be applied.

  5. Question 5Beginner

    Controlling Program Flow · Create and use loops

    A developer is implementing a custom collection class that must be usable in a for-each loop. Which interface must the custom class implement to support this functionality?

    Show answer & explanation

    Correct answer: C

    The for-each loop (or enhanced for loop) in Java works with any object whose class implements the java.lang.Iterable interface. This interface has a single abstract method, iterator(), which must return an instance of java.util.Iterator. The loop then uses this iterator to traverse the elements.

  6. Question 6Intermediate

    Working with Arrays and Collections · Use List, Set, Map and Deque collections

    What is the output of the following code snippet?

    var list = new java.util.ArrayList ();
    list.add("A");
    list.add("B");
    list.add("C");
    
    for (var s : list) {
    System.out.print(s);
    list.remove(s);
    }
    
    Show answer & explanation

    Correct answer: B

    It is illegal to modify a collection (e.g., by calling list.remove(s)) while iterating over it using an enhanced for-loop or a standard iterator, except through the Iterator's own remove method. The code will print "A", then attempt to remove it. On the next iteration, the iterator's hasNext() or next() method will detect that the underlying collection has been structurally modified and will throw a ConcurrentModificationException to prevent unpredictable behavior.

  7. Question 7Beginner

    Database Applications with JDBC · Connect to and perform database SQL operations

    A database administrator needs a Java application to connect to an Oracle database. The connection details are: driver oracle.jdbc.driver.OracleDriver, URL jdbc:oracle:thin:@host:1521:sid, user scott, and password tiger. Which code fragment correctly establishes a connection to the database?

    Show answer & explanation

    Correct answer: A

    Since JDBC 4.0, it is no longer necessary to explicitly load the driver class using Class.forName(). The DriverManager can discover and load drivers automatically from the classpath. The most direct and modern way to establish a connection is to call DriverManager.getConnection(url, user, password).

  8. Question 8IntermediateSelect 3

    Java Object-Oriented Approach · Make objects immutable

    A developer is creating an immutable User class. Which of the following principles must be followed to ensure true immutability? (Select THREE)

    Show answer & explanation

    Correct answers: A, B, C

  9. Question 9Advanced

    Concurrency · Develop thread-safe code

    Case Study

    Company Background:
    GlobalTrade Inc. is a logistics company that provides real-time tracking for international shipments. They are developing a new backend service, TrackingService, using Java 11. This service receives location updates from various carriers, validates them, and stores them in a database. Performance and reliability are critical, as the service must handle thousands of updates per second.

    Current Situation:
    The development team has created a LocationUpdateProcessor class that runs in its own thread. This processor fetches batches of raw location data from a shared queue, processes them, and writes them to the database. During initial load testing, the team observed that under heavy load, the application sometimes throws a NullPointerException within the run() method of the processor. The code for the processor is as follows:

    class LocationUpdateProcessor implements Runnable {
    private volatile boolean isRunning = true;
    private final Queue dataQueue;
    
    public LocationUpdateProcessor(Queue queue) { this.dataQueue = queue; }
    
    public void stopProcessing() { isRunning = false; }
    
    @Override
    public void run() {
    while (isRunning) {
    String rawData = dataQueue.poll();
    // NPE occurs on the next line
    if (rawData.contains("URGENT")) {
    // process urgent data
    }
    }
    }
    }
    

    Requirements:
    The LocationUpdateProcessor must be fixed to prevent the NullPointerException. It should process data as long as the isRunning flag is true and there is data in the queue. The solution must be efficient and avoid busy-waiting (i.e., continuously looping and checking the queue when it's empty).

    Question:
    Which modification to the run() method is the most appropriate and robust solution to fix the bug and meet the requirements?

    Show answer & explanation

    Correct answer: D

    The NullPointerException occurs because dataQueue.poll() returns null when the queue is empty. The original code does not check for null before calling a method on rawData. While adding a null check fixes the NPE, it leads to busy-waiting, violating a requirement. The most robust and idiomatic solution for producer-consumer scenarios is to use a java.util.concurrent.BlockingQueue. The take() method blocks until an element is available, which efficiently waits without consuming CPU cycles. This fixes the NPE and avoids busy-waiting, fully satisfying the requirements.

  10. Question 10Beginner

    Working with Java data types · Use local variable type inference

    What is the purpose of the var keyword introduced in Java 10 and available in Java 11?

    Show answer & explanation

    Correct answer: B

    The var keyword is used for local variable type inference. It is purely a compile-time feature that allows the developer to omit the explicit type declaration for a local variable when it can be inferred from the right-hand side of the assignment. The variable is still statically typed; its type is fixed at compile time and cannot change.

Ready for the real thing?

The full 1Z0-819 simulator has every exam-style question, timed mode, and instant scoring.