Skip to content

1Z0-819 Java SE 11 Developer (OCP) Practice Questions

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

235 questions in the full set20 sample questionsUpdated Jan 23, 2026

Unlock the full exam and previous versions

  • v1Version 1 235 questions Current
  • 1z0-816Legacy Java SE 11 Programmer II 80 questions Locked
Exam fee
$245 USD
Level
Professional
Valid for
Non-expiring (certification does not expire but may become outdated)
Domains covered on the exam 13
  1. Working with Java data types
  2. Controlling Program Flow
  3. Java Object-Oriented Approach
  4. Exception Handling
  5. Working with Arrays and Collections
  6. Working with Streams and Lambda expressions
  7. Java Platform Module System
  8. Concurrency
  9. Java I/O API
  10. Secure Coding in Java SE Application
  11. Database Applications with JDBC
  12. Localization
  13. Annotations
  1. 1

    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 details

    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.

  2. 2

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

    Show answer details

    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.

  3. 3

    You are given a Stream >. Your task is to transform this into a Stream containing all the strings from all the lists. Which stream operation should you use?

    Show answer details

    Correct answer: B

    The flatMap operation is used to transform each element of a stream into another stream and then flatten the resulting streams into a single stream. In this case, each List is mapped to a Stream by list.stream(), and flatMap combines all these smaller streams into one Stream .

  4. 4

    Given the following interface and class definitions:

    interface Rideable {
    default String ride() { return "Riding"; }
    }
    
    interface Transport {
    static String travel() { return "Traveling"; }
    }
    
    class Vehicle implements Rideable, Transport {
    // line n1
    }
    

    Which statement is true?

    Show answer details

    Correct answer: B

    Default methods (like ride()) are inherited by implementing classes. Static methods in interfaces (like travel()) are not inherited by implementing classes; they can only be called on the interface itself (e.g., Transport.travel()). Therefore, a Vehicle object will have the ride() method but you cannot call vehicle.travel().

  5. 5

    A developer needs to serialize an object of the Employee class. However, the Employee class contains a field transient private String securityToken; that should not be included in the serialized form. What is the effect of the transient keyword in this context?

    Show answer details

    Correct answer: B

    The transient keyword is a modifier used to indicate that a field should not be part of the default serialized form of an object. When an object is serialized, the values of its transient fields are ignored. Upon deserialization, the transient field will be initialized to its default value (e.g., null for objects, 0 for numeric primitives).

  6. 6

    True or False: A module can require another module and also export a package from that same required module in its module-info.java file.

    Show answer details

    Correct answer: B

    This statement is false. A module can only export packages that it contains. It cannot re-export a package from another module it depends on. To expose the API of a dependency to other modules that depend on the current module, the requires transitive directive must be used.

  7. 7

    Which of the following code snippets correctly uses a java.util.function.Predicate to filter a list of integers, keeping only the even numbers?

    Show answer details

    Correct answer: A

    A java.util.function.Predicate is a functional interface that represents a predicate (a boolean-valued function) of one argument. It has a single abstract method test(T t). The Stream.filter() method accepts a Predicate. This code correctly defines a Predicate using a lambda expression n -> n % 2 == 0 and then passes it to the filter method to process the stream.

  8. 8

    A developer is tasked with creating a method that accepts a java.nio.file.Path object and returns a java.io.BufferedReader for that file. The method must ensure that the file's resources are automatically closed, even if an IOException occurs during reading. Which implementation correctly and most concisely achieves this?

    Show answer details

    Correct answer: C

    The try-with-resources statement is designed for this exact purpose. However, returning the resource from within the try block is problematic because the resource will be closed before the caller can use it. The correct approach is to use a method that returns the stream and rely on the caller to manage its lifecycle, or to perform the reading within the block itself. Given the options, this is a trick question about the scope of try-with-resources. The most correct pattern is not returning the resource, but if forced to choose from flawed options that attempt to solve the problem, the question intent is likely about recognizing the try-with-resources syntax. Correction: The question asks for an implementation that returns a reader. A better implementation would pass a Consumer to process the reader's contents inside the try-with-resources block. However, amongst the given choices, the question is flawed. The most plausible intended answer is the one using Files.newBufferedReader directly, but it must be understood that the resource management is now the caller's responsibility. Let's re-evaluate the question. The question is testing the Files.newBufferedReader method combined with try-with-resources. The only syntactically valid option that uses the modern java.nio.file API and addresses resource management is the one using Files.newBufferedReader. The correct way to use it is to pass the reader to a consumer inside the block. But if the goal is to return it, the try-with-resources block cannot be used in the method itself. The best option among the choices that directly addresses the prompt is to simply return the result of Files.newBufferedReader(p), and document that the caller must close it. Let's assume the question is flawed and seeks the most modern API usage. The factory method Files.newBufferedReader(p) is the most direct way to get a BufferedReader from a Path.

  9. 9

    A development team is working on a modularized Java 11 application for a financial institution. The application has the following module structure:

    • com.finance.datamodel: Exports a package com.finance.datamodel.entities.
    • com.finance.persistence: Requires com.finance.datamodel and handles database operations.
    • com.finance.reporting: Requires com.finance.datamodel and generates reports.

    The team now needs to introduce a new module, com.finance.core, which will contain business logic. This new core module needs access to the persistence layer. To achieve loose coupling, they decide to use services. The com.finance.persistence module will provide a PersistenceService implementation.

    Which combination of directives must be added to the respective module-info.java files to correctly implement this service-based dependency?

    Select TWO.

    Show answer details

    Correct answer: A, B

    To implement the service locator pattern in JPMS, the consumer module (com.finance.core) must declare that it uses the service interface. The provider module (com.finance.persistence) must declare that it provides an implementation for that service interface using the with keyword. This decouples the modules, as the core module does not need a requires directive for the persistence module.

  10. 10

    You are developing a secure REST API endpoint that accepts a user ID as a path parameter to fetch user details from a database. To prevent unauthorized access, the endpoint must validate that the incoming user ID belongs to the currently authenticated principal. The security framework provides the authenticated principal's user ID via SecurityContext.getPrincipalId(). A junior developer has written the initial code. Which code snippet represents the most common security vulnerability in this context?

    Show answer details

    Correct answer: C

    This code snippet is vulnerable to Insecure Direct Object Reference (IDOR). It directly uses the user-provided userId to fetch data from the database without verifying that the authenticated user is authorized to access that specific user's data. An attacker could simply change the userId in the URL to access another user's information. The other options show proper input validation or correct authorization checks.

Create an account to continue.