Oracle Certified Professional Java SE 21 Developer (1Z0-830) Exam-style mock test
Oracle Java SE 21 Developer (1Z0-830) Practice Exams|Practice test
Full explanations
Exam format
Pass line 68%
33 USD One-time · lifetime access
Enroll →
Sample questions
Example1 · Java Language Basics, Types, and Operators
At the SaaS firm Nimbus Billing, a developer declares a field to hold an invoice count: long total = 2_000_000_000 + 2_000_000_000;. What is printed by System.out.println(total);?
✓A negative value, because the right-hand side is computed with int arithmetic before the result is widened and assigned to the long field total.
Correct. Both literals are int, so the addition happens in int precision and overflows to a negative value, which is then widened to long on assignment.
・4000000000
Incorrect. The sum would need long arithmetic; here both operands are int, so overflow occurs before the widening assignment.
・A compile-time error about integer overflow in the addition expression.
Incorrect. Integer overflow is silent in Java and never produces a compile-time error.
・4_000_000_000
Incorrect. Underscores are only allowed inside numeric literals in source code, not in printed output.
Explanation
Arithmetic is performed in the type of the operands. Two int literals produce an int result that overflows before being widened to long.
Example2 · Object-Oriented Programming: Classes, Interfaces, Records, and Sealed Types
At NimbusDesk, a B2B SaaS help-desk platform, a developer models an immutable audit entry:
public record AuditEntry(String actor, int severity) {
public AuditEntry {
if (severity < 0) throw new IllegalArgumentException("bad");
actor = actor.strip();
}
}
What is the effect of the assignment to actor inside the compact constructor?
・It is a compile error because record components are final and cannot be assigned anywhere in the record body.
Incorrect. A compact constructor is allowed to assign to the implicit constructor parameters, so this compiles.
✓It normalizes the value before the implicit assignment to the actor field, so the stored component is the stripped string.
Correct. In a compact constructor you assign to the parameters; the compiler-inserted code then copies those (possibly modified) parameters into the final fields.
・It assigns to a local parameter that is discarded, leaving the field set to the original unstripped argument value.
Incorrect. The assignment is not discarded; the implicit field assignment happens after the compact body using the parameter values.
・It causes infinite recursion by re-invoking the canonical constructor.
Incorrect. Assigning to a parameter does not call any constructor, so there is no recursion.
Explanation
A compact canonical constructor lets you validate and normalize the implicit parameters; the compiler assigns those parameters to the fields at the end.
Example3 · Generics, Collections, Streams, and Lambdas
At a B2B SaaS firm, a utility method sums invoice amounts stored in a collection of Number subtypes. Which method signature compiles and accepts a List<Integer> as well as a List<Double> argument while allowing the elements to be read as Number?
・double total(List<? super Number> amounts)
Incorrect. A lower-bounded wildcard List<? super Number> would not accept List<Integer> or List<Double>, since Integer and Double are not supertypes of Number.
✓double total(List<? extends Number> amounts)
Correct. An upper-bounded wildcard List<? extends Number> accepts any list whose element type is Number or a subtype, and elements can be read as Number.
・<T extends Number> double total(List<T super Number> amounts)
Incorrect. The syntax List<T super Number> is invalid; a type parameter declaration cannot use super in that position.
・double total(List<Number> amounts)
Incorrect. List<Number> is invariant, so it only accepts a List<Number> and rejects List<Integer> and List<Double>.
Explanation
Reading from a producer uses an upper bound (? extends), which permits covariant subtype lists while still exposing elements as the bound type.
Example4 · Exception Handling, Concurrency, and Virtual Threads
A healthcare billing service opens a database connection and a file writer inside a single try-with-resources statement to export claim records. Both resources implement AutoCloseable. The connection is declared first and the writer second in the resource list. In which order does the JVM close these resources when the try block completes normally?
✓The writer is closed first, then the connection, because resources are closed in reverse order of their declaration in the resource list
Correct. Resources in a try-with-resources statement are closed in the reverse order of their declaration, so the writer (declared last) closes before the connection.
・The connection is closed first, then the writer, because resources are closed in the same order in which they were declared in the resource list
Incorrect. Closing does not follow declaration order; it follows reverse declaration order.
・Both resources are closed concurrently once the try block finishes, since the order of closing AutoCloseable resources is unspecified by the language
Incorrect. The order is well specified and sequential, not concurrent, and it is not left unspecified.
・Neither resource is closed automatically unless an explicit finally block invokes close on each resource in turn
Incorrect. Try-with-resources closes every declared resource automatically without any explicit finally block.
Explanation
Try-with-resources guarantees automatic closing in reverse declaration order, which the billing service relies on so the writer flushes before the connection drops.
Example5 · Modules, I/O, and Platform Features (Pattern Matching, Switch)
A healthcare billing engine processes an Object claim that may be an Integer (claim id), a String (patient note), or a Double (amount). The developer writes a switch using type patterns: switch (claim) { case Integer i -> log(i); case String s -> log(s); case Double d -> log(d); }. The compiler reports an error on this switch. Pick 1. What is the cause?
✓A switch using type patterns over a non-sealed reference type must be exhaustive, so a default or total pattern case is required but is missing here.
Correct. A pattern-matching switch over a type that is not sealed and not exhaustively covered requires a default label (or total pattern) to be exhaustive; without it, compilation fails.
・Type patterns are not permitted in switch statements and can only appear in instanceof expressions in Java 21.
Incorrect. Type patterns are explicitly allowed in switch in Java 21 as a standard feature.
・Each case label with a type pattern requires an explicit break statement even when arrow syntax is used.
Incorrect. Arrow-syntax cases do not fall through and never require break.
・The switch selector must be an enum, int, or String; Object selectors are rejected.
Incorrect. Pattern-matching switch allows any reference type selector such as Object.
Explanation
A pattern switch must be exhaustive. Because Object is not sealed and the cases do not cover every possible type, a default (or total) pattern is mandatory.
Example6 · Java Language Basics, Types, and Operators
A developer at a healthcare services firm writes a line to store a patient record identifier that exceeds the range of int: long recordId = 9_000_000_000;. The code fails to compile. Which single change makes this declaration compile correctly?
・Rewrite the right-hand side as an int cast such as (int) 9000000000 so the value fits the variable
Incorrect. Casting to int would discard the high bits and overflow; the goal is to keep the full value in a long.
✓Append the suffix L to the literal, writing long recordId = 9_000_000_000L;
Correct. An integer literal is treated as int by default; the L suffix makes it a long literal so the value within long range compiles.
・Remove the underscores because they are only permitted inside floating-point literals in Java
Incorrect. Underscores are allowed as digit separators in integral literals too, so they are not the cause of the error.
・Change the type to double since no integral type in Java can hold this identifier value
Incorrect. long easily holds 9,000,000,000, so switching to double is unnecessary and would change the semantics.
Explanation
Integer literals default to int; a value above Integer.MAX_VALUE needs the L suffix to be a long literal.
Example7 · Java Language Basics, Types, and Operators
At the retailer NorthPeak Outfitters, a developer writes this line inside a method that totals cart items: var count = 10; Later the team wants count to hold a very large sum that exceeds the int range. Which statement correctly describes the inferred type of count and the needed fix?
・count is inferred as long, so no change is required because long already covers the larger sum range needed.
Incorrect. An untyped integer literal like 10 is an int, not a long, so count is inferred as int.
✓count is inferred as int from the literal 10, so a literal suffix such as 10L is needed to infer long.
Correct. var infers the type from the initializer's compile-time type; 10 is an int literal, so count is int, and writing 10L makes it long.
・var always infers double for numeric literals, so count cannot overflow regardless of the assigned value.
Incorrect. var does not force double; integer literals yield int, and double only results from double initializers.
・var is illegal for numeric literals and the line fails to compile until an explicit primitive type is declared.
Incorrect. var is fully legal with numeric literals; the line compiles and infers int.
Explanation
Local variable type inference uses the initializer's static type. The literal 10 is an int, so var infers int; a long suffix (L) would be required to infer long.
Example8 · Java Language Basics, Types, and Operators
NorthPeak Outfitters packs loyalty points into boxes of 7. Given int points = 45; the code computes int boxes = points / 7; int leftover = points % 7; What values do boxes and leftover hold after these two statements execute?
・boxes is 6 and leftover is 3, because the division rounds the quotient up to the nearest whole box of points.
Incorrect. Integer division truncates toward zero rather than rounding up; 45 / 7 is 6, and 45 % 7 is 3.
・boxes is 6 and leftover is 45, because modulus returns the original operand when it is larger than the divisor.
Incorrect. Modulus does not return the whole operand; 45 % 7 is 3.
✓boxes is 6 and leftover is 3.
Correct. 45 / 7 truncates to 6, and 45 % 7 is the remainder 3.
・boxes is 6.4 and leftover is 3, because int division keeps the fractional part of the quotient while modulus drops it.
Incorrect. int division cannot hold a fractional value; 45 / 7 produces 6, not 6.4.
Explanation
Integer division truncates the fractional part toward zero (45 / 7 = 6), and the modulus operator yields the remainder (45 % 7 = 3).
Example9 · Java Language Basics, Types, and Operators
In NorthPeak Outfitters' pricing code a developer writes: int qty = 3; double unit = 4.5; and then evaluates qty * unit. What is the resulting type and value of the expression qty * unit?
・The result is int 13 because qty drives the arithmetic and the fractional part of unit is discarded before multiplication.
Incorrect. The int operand is promoted to double, not the other way around; no truncation occurs here.
・The expression fails to compile because int and double cannot be multiplied together without an explicit cast.
Incorrect. Mixing int and double is allowed; the int is promoted automatically.
✓The result is double 13.5 because qty is promoted to double for the multiplication.
Correct. Binary numeric promotion widens the int qty to double, so 3.0 * 4.5 yields double 13.5.
・The result is double 12.0 because unit is narrowed to its integer part first and then multiplied by qty.
Incorrect. unit is not narrowed; the int is promoted, producing 13.5 rather than 12.0.
Explanation
When an int and a double are operands of a binary arithmetic operator, the int is promoted to double, so qty * unit is 3.0 * 4.5 = 13.5 of type double.
Example10 · Java Language Basics, Types, and Operators
At NorthBridge Capital, a developer writes a settlement module and declares: long accountId = 1_000_000_000_000; The compilation fails. Reviewing the code, which statement correctly explains the failure and its fix?
・The underscores are illegal inside a numeric literal, so each underscore must be removed before the value can compile.
Incorrect. Underscores are permitted between digits of a numeric literal, so they are not the cause of the failure.
✓The literal is parsed as an int and exceeds Integer.MAX_VALUE; appending an L suffix makes it a long literal.
Correct. Integer literals are int by default; 1_000_000_000_000 overflows int, and the L suffix declares it as a long literal.
・A long variable cannot hold a value that large, so the type must be changed to double for this amount.
Incorrect. A long can hold this value easily; changing to double is unnecessary and would lose integer precision.
・The literal is valid, so the real problem is that accountId should be declared final here.
Incorrect. The literal does not compile at all, and adding final would not change the overflow problem.
Explanation
An integer literal is treated as int unless suffixed with L (or l). Values beyond the int range need the L suffix to be interpreted as long.
Example11 · Java Language Basics, Types, and Operators
A trading-desk utility class at NorthBridge Capital contains the line: var rate; followed later by rate = 0.05;. The developer expects type inference to work. Why does this fail to compile?
✓var requires an initializer in the same declaration statement so the compiler can infer the variable's type.
Correct. A var declaration must include an initializer so the compiler can infer the type at that point.
・var can only be used for fields, not for any local variable inside a method body.
Incorrect. var is actually restricted to local variables and is not permitted on fields at all.
・var defaults rate to the type Object, which cannot later be reassigned a double value.
Incorrect. var never defaults to Object; it has no inferred type without an initializer, so this is wrong.
・var is only valid when the inferred type is a reference type and never a primitive.
Incorrect. var works with primitives too, so for example var x = 0.05; infers double correctly.
Explanation
var relies on an initializer to infer the type. A bare var declaration with no initializer is a compile error.
Example12 · Java Language Basics, Types, and Operators
A developer on the warehouse inventory team writes a method that iterates over pallet SKUs. Inside the method body they declare: var sku = "MX-4471"; var qty = 0; var discount; At compile time, what happens?
・All three declarations compile, and var infers String, int, and Object respectively based on each context
Incorrect. var cannot default to Object for an uninitialized variable; without an initializer there is nothing to infer from.
✓The first two declarations compile, but the third fails because var requires an initializer to infer the type
Correct. var requires an initializer at the point of declaration; sku and qty compile, but discount has no initializer so the compiler cannot infer its type and rejects it.
・All three declarations fail because var is only permitted on instance fields, not on local variables inside methods
Incorrect. var is restricted to local variables (and some loop and lambda contexts), not instance fields, so this is the opposite of the rule.
・Only the first declaration compiles because var can infer reference types but never primitive types such as int
Incorrect. var infers primitive types such as int perfectly well; qty = 0 is inferred as int.
Explanation
Local-variable type inference with var demands an initializer so the compiler can determine the type; var cannot be used for a bare declaration, fields, or method parameters.
Example13 · Java Language Basics, Types, and Operators
A billing routine for a parts distributor computes how many full shipping boxes are needed and the leftover items: int items = 47; int perBox = 10; int boxes = items / perBox; int leftover = items % perBox; What values do boxes and leftover hold?
・boxes is 5 and leftover is 0 because integer division rounds the quotient up to the nearest whole box
Incorrect. Integer division truncates toward zero rather than rounding up, so the quotient is 4, not 5.
✓boxes is 4 and leftover is 7
Correct. 47 / 10 truncates to 4, and 47 % 10 yields the remainder 7.
・boxes is 4.7 and leftover is 7 because the division produces a decimal quotient before truncation
Incorrect. Dividing two int operands yields an int result; there is no intermediate decimal value of 4.7 in integer arithmetic.
・The code fails to compile because the modulo operator cannot be applied to two int operands
Incorrect. The modulo operator is valid for int operands and produces the remainder, so the code compiles.
Explanation
With two int operands, / truncates toward zero and % returns the remainder; 47/10 is 4 and 47%10 is 7.
Example14 · Java Language Basics, Types, and Operators
At a telecom operator, a billing module stores an active-subscriber count in an int field. During a surge, code assigns it the literal 2_200_000_000 to represent projected national subscribers. What happens when this statement is compiled?
・The code runs but the field silently wraps around to a negative value at runtime.
Incorrect. Wrap-around happens for arithmetic overflow at runtime, not for an int literal whose written value already exceeds Integer.MAX_VALUE.
✓A compile-time error occurs because the literal exceeds the maximum value an int can hold.
Correct. 2_200_000_000 is greater than Integer.MAX_VALUE (2_147_483_647), so the int literal is out of range and the compiler rejects it.
・The literal is automatically widened to a long and the assignment succeeds without any warning.
Incorrect. Java does not auto-promote an out-of-range int literal to long; an L suffix would be required and is absent here.
・The underscores in the literal are rejected first, before the magnitude is ever checked.
Incorrect. Underscores between digits are legal grouping separators here, so they are not rejected; the magnitude is the problem.
Explanation
An integer literal written without an L suffix must fit in int. 2.2 billion exceeds Integer.MAX_VALUE, causing a compile-time error.
Example15 · Java Language Basics, Types, and Operators
A network-usage report computes `byte a = 100; byte b = 100; int total = a * b;` for two cell towers at the telecom operator. What is the value of total?
・A compile-time error, because multiplying two byte variables is not permitted in Java.
Incorrect. Multiplying two byte variables is permitted; both are promoted to int, so there is no compile error.
・A value that has overflowed, since the product is computed using byte arithmetic.
Incorrect. The multiplication is performed in int, not in byte, so no overflow of a byte occurs.
✓10000, because both operands are promoted to int before the multiplication.
Correct. Binary numeric promotion converts both byte operands to int, so 100 * 100 is evaluated as 10000.
・100, because the smaller operand type caps the result of the expression.
Incorrect. Operand types are promoted to int; the expression is not capped at the value of one operand.
Explanation
Binary numeric promotion widens byte operands to int before arithmetic, producing 10000 and storing it safely in an int.
▶ Try 10 free questions (graded)