Home / Oracle Certified Professional: Java SE 11 Developer (1Z0-819) / Mock exam
Oracle Certified Professional: Java SE 11 Developer (1Z0-819) Exam-style mock test

Oracle Java SE 11 Developer (1Z0-819) Practice Exams|Practice test

Full explanations Exam format Pass line 65%
33 USD One-time · lifetime access
Enroll →

Sample questions

Example1 · Java Language & Object-Oriented Programming
At NimbusDesk, a B2B SaaS help-desk platform, a developer writes a method with the local declaration `var tickets = new ArrayList<String>();` and later tries `var backlog;` on its own line before assigning it. The file fails to compile. Why does the second declaration fail while the first succeeds?
✓A local variable declared with var must be initialized on the same line, because the compiler infers the type from the initializer expression and has nothing to infer from otherwise.
Correct. var relies on the initializer expression to infer the static type, so a var local without an initializer gives the compiler nothing to infer and is illegal.
・The var keyword can only be applied to collection types such as ArrayList, and backlog has no type that qualifies for inference by the compiler here.
Incorrect. var works with any reference or primitive type that can be inferred, not only collections; the real issue is the missing initializer.
・A var declaration must always be marked final before the compiler will allow it to be used anywhere inside a method body of this kind.
Incorrect. final is optional with var and has nothing to do with the failure; var simply requires an initializer.
・The identifier backlog clashes with a reserved contextual keyword, so the compiler rejects it during the inference phase of compilation.
Incorrect. var is a reserved type name, not a reserved identifier, so backlog as a variable name is perfectly legal.
Explanation
Local variable type inference with var requires an initializer in the same statement because the type is derived from the right-hand side; `var backlog;` has no expression to infer from and does not compile.
Example2 · Generics, Collections & Streams
A billing service at a B2B SaaS company has a method that must sum the amounts of any list whose elements are Number or a Number subtype, such as List<Integer> or List<Double>. Which method signature compiles and accepts all of those argument types while reading the values safely?
・public static double total(List<? super Number> items) reading each element as a Number
Incorrect. A lower-bounded wildcard List<? super Number> accepts List<Number> or List<Object> but rejects List<Integer>, so the required subtype lists cannot be passed.
✓public static double total(List<? extends Number> items) reading each element as a Number
Correct. An upper-bounded wildcard List<? extends Number> accepts List<Integer>, List<Double>, and List<Number>, and every element can be read safely as a Number.
・public static double total(List<Number> items) reading each element as a Number value
Incorrect. The invariant type List<Number> only accepts a List<Number>; a List<Integer> argument is a compile error because generics are not covariant.
・public static double total(List<?> items) casting each element to Number before use
Incorrect. The unbounded wildcard List<?> forces an unchecked cast and offers no compile-time guarantee that elements are Numbers at all.
Explanation
Reading values from a producer calls for an upper-bounded wildcard (? extends Number), following the Producer Extends, Consumer Super guideline.
Example3 · Functional Programming & Lambdas
A billing service at a B2B SaaS company declares a functional interface InvoiceRule with a single abstract method boolean applies(Invoice i). A developer needs a lambda that returns true when an invoice total exceeds 1000. Which assignment compiles and behaves correctly?
✓InvoiceRule r = i -> i.getTotal() > 1000;
Correct. A single-parameter lambda may omit the parameter type and parentheses, and an expression body implicitly returns its boolean value matching applies.
・InvoiceRule r = (Invoice i) { return i.getTotal() > 1000; };
Incorrect. A block body must be preceded by the arrow token, and this form omits the -> entirely so it fails to compile as a lambda.
・InvoiceRule r = i -> { return i.getTotal() > 1000 };
Incorrect. A block-bodied lambda requires a semicolon after the return statement, and omitting it causes a compilation error inside the braces.
・InvoiceRule r = (i) -> i.getTotal() > 1000 && return true;
Incorrect. The return keyword is a statement and cannot appear inside an expression combined with the && operator, so this does not compile.
Explanation
A conforming lambda for a single-method functional interface can use the concise expression form where the body value is returned implicitly; block bodies require braces, explicit return, and a terminating semicolon.
Example4 · Concurrency, Modules & I/O
At a healthcare services firm, several scheduler threads concurrently update a shared map of clinic-id to current patient count. The lead developer replaces a plain HashMap with a thread-safe collection and now needs atomic per-key increments without surrounding synchronized blocks. Which single call correctly performs the atomic increment on a ConcurrentHashMap<String,Integer> named counts?
✓counts.compute(clinicId, (k, v) -> v == null ? 1 : v + 1)
Correct. compute performs the read-modify-write atomically for the given key, so concurrent increments do not lose updates even without external synchronization.
・counts.put(clinicId, counts.getOrDefault(clinicId, 0) + 1)
Incorrect. The separate getOrDefault then put is a non-atomic compound action; two threads can read the same value and one increment is lost.
・counts.replace(clinicId, counts.get(clinicId) + 1)
Incorrect. replace is atomic only for the swap itself, but the get used to build the new value happens outside it, so the overall increment races.
・counts.putIfAbsent(clinicId, counts.get(clinicId) + 1)
Incorrect. putIfAbsent only stores a value when the key is missing, so it will not increment an already-present count.
Explanation
ConcurrentHashMap offers atomic merge/compute methods precisely so compound read-modify-write operations on a key become a single atomic action; chaining separate get and put calls reintroduces a race.
Example5 · Exceptions, JDBC & Secure Coding
A healthcare services firm opens a patient-record file inside a try-with-resources block. The resource's close() method throws an IOException, and the body of the try block also throws a separate IOException while reading records. Which exception propagates out of the method, and what happens to the other?
・The exception from close() propagates, and the exception thrown in the try body is silently discarded by the runtime without any trace.
Incorrect. The try-body exception, not the close() exception, is the primary one; the close() exception is suppressed, not the reverse.
✓The exception thrown in the try body propagates, and the one from close() is attached to it as a suppressed exception.
Correct. The exception from the try body is primary and propagates, while the close() exception is added to it and retrievable via getSuppressed().
・Both exceptions are wrapped together into a new RuntimeException that the method then throws to the caller.
Incorrect. The runtime does not wrap the two into a new RuntimeException; the original try-body exception propagates unchanged.
・The method throws a MultiCatchException containing references to both of the original IOException instances.
Incorrect. There is no MultiCatchException type in Java; suppression attaches the close() exception to the primary one.
Explanation
In try-with-resources, an exception from the try body takes precedence; any exception thrown during automatic close() is suppressed and accessible through Throwable.getSuppressed().
Example6 · Java Language & Object-Oriented Programming
At MercyCare Health Systems, a developer writes a method that loops over patient visit records. Inside the method body she writes: var total = 0; for (var i = 0; i < visits.length; i++) { total += visits[i].cost(); }. Which statement correctly describes the use of var here?
✓The code compiles, and both total and i are inferred as int because each has an initializer providing a non-null type at its declaration point.
Correct. var requires an initializer, and both total (0) and the for-loop variable i (0) have int initializers, so each is inferred as int and the loop compiles.
・The code fails to compile because var is only permitted as a field type, not for local variables declared inside the body of a method.
Incorrect. var is a local-variable type inference feature; it is NOT allowed for fields, but it IS allowed for local variables, so this claim is backwards.
・The code fails to compile because var cannot be used in the initialization section of a traditional for loop statement in any Java release.
Incorrect. var is explicitly permitted in the init clause of a traditional for loop as a local-variable declaration, so this is not a compile error.
・The code compiles, but total is inferred as Object since numeric literals assigned through var always widen to the common Object supertype.
Incorrect. var infers the static type of the initializer expression; the literal 0 is an int, so total is int, never Object.
Explanation
var infers each local variable's type from its initializer's static type; integer literals are int, so both total and i become int and the loop compiles normally.
Example7 · Java Language & Object-Oriented Programming
A developer on the UrbanThread retail platform writes a method body containing the line: var cartTotal = 0;. They later assign cartTotal = 49.95; and the compiler reports an error. Why does this assignment fail?
✓The literal 0 makes cartTotal an int, so a double literal cannot be assigned to it later
Correct. The initializer 0 is an int literal, so var infers int for cartTotal, and 49.95 is a double that cannot be assigned to an int without a cast.
・The var keyword forbids any reassignment of the variable after it has been declared and initialized
Incorrect. var declares a regular local variable that can be reassigned; it only prevents changing the inferred type.
・Using var without an explicit type annotation is only permitted for fields, not for local method variables
Incorrect. It is the reverse: var is allowed for local variables but not for fields, and that is not the cause here.
・The compiler cannot infer a numeric type from 0 because var requires a cast on the initializer
Incorrect. var infers int perfectly well from 0 and needs no cast on the initializer; the error is the later double assignment.
Explanation
var infers the static type from the initializer at compile time. Since 0 is an int, cartTotal becomes int and cannot hold a double value later.
Example8 · Java Language & Object-Oriented Programming
The UrbanThread checkout class keeps a private field discountRate and exposes only a public getter plus a validating setter that rejects negative values. A teammate wants to guarantee no code outside the class can place an invalid value into the field. Which access modifier should discountRate keep to best support this encapsulation goal?
・protected, so that only subclasses and classes in the same package may read and write the field
Incorrect. protected still lets subclasses and same-package classes bypass the setter and write invalid values.
✓private, so that the field is reachable only within its own class and all external access goes through the validating setter
Correct. private restricts the field to its own class, forcing all external writes through the validating setter and preserving the invariant.
・public, because the validating setter already guards the value no matter who writes to the field directly
Incorrect. public lets any code assign the field directly, bypassing the setter entirely and defeating the validation.
・package-private, so that any class in the same package can write the field while others cannot
Incorrect. package-private still allows same-package classes to write the field directly without using the setter.
Explanation
Encapsulation is achieved by making the field private so that the only path to change it is the validating setter, which enforces the invariant.
Example9 · Java Language & Object-Oriented Programming
A Product class for UrbanThread defines Product(String sku) and Product(String sku, double price). The developer wants the one-argument constructor to reuse the two-argument constructor by passing a default price of 0.0. Which statement correctly placed as the first line of Product(String sku) achieves this?
・this.Product(sku, 0.0); referencing the current instance to invoke the sibling constructor by its name
Incorrect. Constructors are not invoked by name with a dot syntax; this.Product(...) is not valid Java.
・super(sku, 0.0); so the superclass handles both the sku and the default price assignment for the object
Incorrect. super(...) calls a superclass constructor, not a sibling constructor in the same class.
✓this(sku, 0.0); delegating to the two-argument constructor of the same class with a default price
Correct. this(sku, 0.0) performs constructor chaining to the two-argument constructor of the same class and must be the first statement.
・Product(sku, 0.0); calling the constructor by its class name to forward the remaining argument value
Incorrect. You cannot call a constructor by writing its class name like a method; this is a compile error.
Explanation
Constructor chaining within the same class uses this(args) as the first statement, delegating to the matching overloaded constructor.
Example10 · Java Language & Object-Oriented Programming
A developer at a financial services firm writes a method that computes a daily interest rate and declares a local variable with 'var rate = 0.0375;'. A colleague reviewing the code asks what type the compiler infers for 'rate'. Which statement correctly describes the inferred type?
・The type is inferred as float because the literal is a decimal value used for a rate percentage
Incorrect. A float literal would require an 'f' or 'F' suffix such as 0.0375f; without it the literal is not a float.
✓The type is inferred as double because an unsuffixed floating-point literal defaults to double
Correct. An unsuffixed floating-point literal like 0.0375 is a double, so 'var' infers the static type double at the declaration.
・The type is left undetermined until the variable is first reassigned to another value
Incorrect. Local variable type inference with 'var' fixes the type at compile time from the initializer; it is never deferred to a later reassignment.
・The type is inferred as BigDecimal to preserve precision in all financial calculations
Incorrect. The compiler never silently promotes a primitive literal to BigDecimal; that would require explicit object construction such as new BigDecimal("0.0375").
Explanation
With 'var', the compiler infers the type from the initializer expression at compile time. An unsuffixed decimal literal is of type double, so 'rate' is a double.
Example11 · Java Language & Object-Oriented Programming
A banking team models an Account class and wants to prevent external code from setting a negative balance directly. The 'balance' field is declared private, and all changes go through a deposit and withdraw method that validate the amount. Which object-oriented principle is being applied here?
・Polymorphism, because the deposit and withdraw methods override behavior that is defined in a parent account class
Incorrect. Polymorphism concerns one reference invoking different implementations at runtime, not hiding and guarding a field.
・Inheritance, because the Account class reuses validation logic that was first defined in a more general superclass
Incorrect. Inheritance is about a subclass acquiring members from a superclass; nothing here describes a parent-child class relationship.
✓Encapsulation, because internal state is hidden and modified only through controlled accessor and mutator methods
Correct. Hiding the field as private and exposing only validated methods to read or change it is the definition of encapsulation.
・Abstraction, because the account hides details
Incorrect. Abstraction is about exposing essential behavior while hiding implementation complexity, not specifically about guarding a private field through methods.
Explanation
Making state private and forcing all access through validating methods is encapsulation, which protects invariants such as a non-negative balance.
Example12 · Java Language & Object-Oriented Programming
At a manufacturing distributor, a shipping module computes a pallet weight as int totalGrams = 2_000_000_000; then adds int extra = 500_000_000; using int sum = totalGrams + extra;. What is the result stored in sum?
✓The int addition silently overflows and sum wraps around to a negative value because the mathematical result exceeds Integer.MAX_VALUE.
Correct. int arithmetic performs no overflow checking; when the true result exceeds Integer.MAX_VALUE the bits wrap around in two's complement, yielding a negative number.
・The compiler rejects the statement because the sum of two int literals cannot be assigned back to an int variable.
Incorrect. Adding two int values produces an int, which is a valid assignment to an int variable, so this compiles fine.
・An ArithmeticException is thrown at runtime when the addition exceeds the maximum int range.
Incorrect. Integer overflow in Java does not throw ArithmeticException; only operations like integer division by zero do.
・The value is automatically promoted to long and sum holds the correct total of 2_500_000_000.
Incorrect. There is no automatic promotion to long here because both operands are int; promotion would require a long operand or an explicit cast.
Explanation
Integer overflow in Java is silent and wraps using two's complement, producing a negative value rather than an exception or auto-promotion.
Example13 · Java Language & Object-Oriented Programming
In an inventory report class for the distributor, a developer writes var skuCount = 0; on one line and later var warehouse; on another line without assigning a value. What happens when the file is compiled?
・Both declarations compile, and var warehouse is simply inferred as Object until a value is later assigned to it.
Incorrect. var is never defaulted to Object; without an initializer the compiler has no expression to infer the type from, so it is an error.
・Both declarations compile because var always defers type inference until the first assignment at runtime.
Incorrect. var inference happens at compile time from the initializer expression, not at runtime, and it requires an initializer.
✓Compilation fails only on var warehouse; var cannot be used without an initializer because the type cannot be inferred.
Correct. var requires an initializer in the same statement so the compiler can infer the type; var warehouse; alone fails to compile.
・Compilation fails on both lines because var is not permitted for local variable declarations in method bodies.
Incorrect. var is explicitly allowed for local variables inside method bodies; var skuCount = 0; is perfectly valid.
Explanation
Local variable type inference with var demands an initializer in the same declaration; a bare var declaration cannot be compiled.
Example14 · Java Language & Object-Oriented Programming
A developer at a telecom operator writes `var subscriberCount = 5_000;` inside a method that tallies active SIM cards. Later a colleague asks what inferred type `subscriberCount` has and whether `var` can be reused for a field. What is the correct statement?
✓The variable is inferred as int, and var is permitted only for local variables with an initializer, not for fields.
Correct. An integer literal without a suffix is int, so var infers int, and var is restricted to local variables that have an initializer.
・The variable is inferred as long because the underscore numeric literal always widens the value to a long type.
Incorrect. Underscores are only digit separators and do not change the literal type; 5_000 without an L suffix is an int.
・The variable is inferred as int, but var may also be freely used to declare instance fields of the class.
Incorrect. var cannot be used for instance or static fields; it is limited to local variables, indexes, and similar local contexts.
・The variable fails to compile because var cannot be combined with an underscore-separated numeric literal.
Incorrect. Underscores are legal digit separators inside numeric literals and work fine with var.
Explanation
var infers the static type from the initializer (int here) and is only allowed for local variable declarations that are initialized.
Example15 · Java Language & Object-Oriented Programming
A billing module at the operator computes `byte b = 40; byte result = b * 2;`. The build fails on the second line. Which explanation and fix correctly describes the situation?
✓The expression b * 2 is promoted to int, so assigning it to a byte requires an explicit cast such as (byte)(b * 2).
Correct. Binary numeric promotion converts both operands to int, so b * 2 is an int and must be explicitly cast back to byte.
・The multiplication overflows the byte range at runtime, so the value must be stored in a short rather than a byte variable.
Incorrect. The problem is a compile-time type mismatch from promotion, not a runtime overflow in a short.
・The literal 2 is treated as a long, forcing a narrowing conversion that the compiler refuses to perform without a cast.
Incorrect. The literal 2 is an int, not a long; the promotion to int is what causes the mismatch.
・The error happens because byte arithmetic is unsupported; replace both byte declarations with int to compile it.
Incorrect. byte arithmetic is supported; it simply promotes to int, and a cast resolves it without changing all declarations.
Explanation
Arithmetic on byte/short operands undergoes binary numeric promotion to int, so the int result needs an explicit cast to be stored back in a byte.
▶ Try 10 free questions (graded)
Measure your readiness with a full mock exam
All questions · full explanations · lifetime access
Enroll(33 USD)