Oracle Certified Professional: Java SE 17 Developer (1Z0-829) Exam-style mock test
Oracle Certified Professional: Java SE 17 Developer (1Z0-829)|Practice test
Full explanations
Exam format
Pass line 70%
33 USD One-time · lifetime access
Enroll →
Sample questions
Example1 · Language Basics, Flow Control and Data Types
A ledger module compiles this fragment:
var fee = 1_000;
var rate = 0.025;
var total = fee * rate;
System.out.println(total);
What is printed?
・25
Incorrect. The result is a double because one operand is a double, so a decimal point is printed rather than an integer.
・Compilation fails because var cannot be used with an underscore literal
Incorrect. Underscores are legal digit separators in integer literals and var happily infers int from 1_000.
✓25.0
Correct. fee is inferred as int (1000) and rate as double, so int*double promotes to double and 1000 * 0.025 yields 25.0.
・Compilation fails because total is inferred as int and loses the fraction
Incorrect. total is inferred as double here since numeric promotion makes the multiplication a double expression, not int.
Explanation
With var, the type is inferred from the initializer's static type. Multiplying an int by a double promotes the int operand, producing a double, so the output includes the fractional part as 25.0.
Example2 · Object-Oriented Programming
A payments team models a settled transfer with a record. What is the result of compiling and running this code?
public record Transfer(long cents, String ref) {
public Transfer {
if (cents < 0) throw new IllegalArgumentException("neg");
ref = ref.strip();
}
}
public class Main {
public static void main(String[] a) {
Transfer t = new Transfer(500, " RX-9 ");
System.out.println(t.cents() + ":" + t.ref() + ":" + t.ref().length());
}
}
✓500:RX-9:4
Correct. The compact constructor strips the whitespace before the implicit field assignment runs, so ref becomes "RX-9" with length 4 and cents stays 500.
・Compilation fails because the compact constructor may not assign to the component ref
Incorrect. A compact constructor is allowed to reassign the implicit fields; the normalized value is used when the generated canonical constructor stores the components.
・500: RX-9 :7
Incorrect. The strip() call in the compact constructor removes surrounding spaces, so the stored ref is not the original padded string of length 7.
・Compilation fails because a record requires an explicit canonical constructor
Incorrect. Records auto-generate a canonical constructor; a compact constructor is a valid shorthand and does not require writing the full canonical form.
Explanation
In a compact constructor you validate and normalize parameters, and the generated code then assigns the (possibly reassigned) parameters to the final fields. Here ref.strip() runs before the implicit assignment, producing "RX-9".
Example3 · Exceptions, Generics and Collections
A payment gateway uses the resource below. What is printed?
class Ledger implements AutoCloseable {
Ledger() { System.out.print("open "); }
public void close() { System.out.print("close "); }
void post() { System.out.print("post "); }
}
public class Settle {
public static void main(String[] a) {
try (Ledger l = new Ledger()) {
l.post();
throw new RuntimeException("x");
} catch (RuntimeException e) {
System.out.print("caught ");
} finally {
System.out.print("done");
}
}
}
・The console shows open post caught close done printed in exactly that order
Incorrect. close() runs before the catch block, not after it, because try-with-resources closes the resource while unwinding out of the try body.
✓The console shows open post close caught done printed in exactly that order
Correct. The resource is closed as soon as the try block exits (printing close) before any catch runs, then the catch prints caught, then finally prints done.
・The console shows open post caught done and the exception is then rethrown by finally
Incorrect. finally does not rethrow here; the RuntimeException was already handled by the catch, so execution completes normally after done.
・The console shows open post close done because the catch block body is never reached
Incorrect. The thrown RuntimeException is caught, so the catch body does execute and caught is printed.
Explanation
In try-with-resources, close() is invoked implicitly as control leaves the try block, which happens before the matching catch clause is entered. The order is open, post, close, caught, done.
Example4 · Functional Programming and Streams
A catalog service uses this helper:
import java.util.function.*;
public class SkuTool {
public static void main(String[] args) {
Function<String, Integer> len = String::length;
Function<Integer, String> tag = n -> "SKU-" + n;
System.out.println(len.andThen(tag).apply("radio"));
}
}
What is printed?
・The program prints the value SKU-radio here
Incorrect. andThen does not pass the original String to tag; it passes the Integer result of len.
✓The program prints the text SKU-5
Correct. andThen applies len first ("radio" has length 5), then tag turns 5 into "SKU-5".
・The program prints the bare number 5
Incorrect. The tag function runs after len, so the final result is a String, not the bare Integer 5.
・The program prints the text radio-5
Incorrect. No concatenation of the original argument with the length occurs here.
Explanation
Function.andThen(after) runs the current function first and feeds its result into after. len produces 5, then tag produces "SKU-5".
Example5 · Concurrency, I/O and Platform
A catalog indexing service runs this code:
ExecutorService pool = Executors.newFixedThreadPool(2);
Future<Integer> f = pool.submit(() -> {
Thread.sleep(50);
return 7 * 6;
});
System.out.println(f.get());
pool.shutdown();
Assuming all required imports are present and no exception is thrown, what does the program print?
✓42 is printed once get() returns the completed task result
Correct. The lambda returns a value, so it is a Callable<Integer>; Future.get() blocks until the task finishes and returns 42.
・0 is printed because get() returns before the task finishes computing
Incorrect. Future.get() is a blocking call that waits for completion, so it never returns a stale or zero value here.
・The submit call fails to compile since a lambda can never be a Callable here
Incorrect. A lambda matching Callable's call() signature (no args, returns a value) is a valid Callable, so it compiles fine.
・Nothing prints because the pool must be shut down before get() is invoked
Incorrect. get() can be invoked on a submitted task without shutting the pool down first; shutdown merely stops accepting new tasks.
Explanation
A lambda that returns a value and takes no arguments is inferred as Callable<Integer> when passed to submit. Future.get() blocks until the task completes and yields the computed 42.
Example6 · Modules, Security and Localization
The streaming service's playback module declares:
module stream.playback {
requires stream.codec;
exports stream.playback.api;
opens stream.playback.internal;
}
At runtime a reflection library in another module calls setAccessible(true) on a private field of a class in the stream.playback.api package. What is the result?
・The reflective access succeeds because exporting a package automatically permits deep reflection into every public type and member it contains.
Incorrect. exports grants normal access to public members of public types only; it never enables setAccessible on private members.
・The module fails to load because a package cannot be both exported and reflectively accessed within one descriptor.
Incorrect. A package may be both exported and opened; there is no conflict, and here only stream.playback.internal is opened, not the api package.
・The reflective access succeeds only when the calling module also declares a requires transitive edge to stream.playback.
Incorrect. requires transitive affects readability propagation, not the ability to perform deep reflection, which depends solely on opens.
✓The reflective access throws InaccessibleObjectException because exports does not grant deep reflective access.
Correct. exports makes public types accessible but does not open the package; deep reflection on private members requires opens (or an open module), so setAccessible fails with InaccessibleObjectException.
Explanation
In JPMS, exports controls compile and run-time access to public API, while opens controls deep reflective access (setAccessible). Because stream.playback.api is only exported and not opened, reflection on private members throws InaccessibleObjectException.
Example7 · Language Basics, Flow Control and Data Types
A telemetry collector snippet computes a running figure:
int sensors = 4;
double load = sensors / 3 * 2.0;
System.out.println(load);
What does this program print?
✓2.0
Correct. sensors / 3 is integer division yielding 1, then 1 * 2.0 promotes to double, so 1.0 * 2.0 equals 2.0.
・2.6666666666666665
Incorrect. That value would arise only if the whole expression used floating point from the start, which is not the case here.
・The code fails to compile because mixed types are used in one expression
Incorrect. Java readily promotes int to double during evaluation, so mixing int and double literals compiles fine.
・2.666666...
Incorrect. No repeating decimal appears because integer division happened before any promotion to double.
Explanation
Operators of equal precedence evaluate left to right, so sensors / 3 is performed as int division (4/3 = 1) before the result is multiplied by 2.0. Promotion to double occurs only at the multiplication, giving 2.0.
Example8 · Language Basics, Flow Control and Data Types
A routing engine builds a label with StringBuilder:
StringBuilder route = new StringBuilder("A->B");
String leg = route.append("->C").reverse().toString();
System.out.println(route.length() + ":" + leg);
What is printed?
・The code fails to compile because a String cannot be assigned the result of a builder chain
Incorrect. toString() returns a String, so assigning that chained result to a String variable is perfectly legal and compiles.
✓7:C<-B<-A
Correct. append and reverse both mutate the same builder in place, so route holds "C<-B<-A" of length 7, and leg references that same text.
・7:C->B->A
Incorrect. reverse() reverses every character including the arrow symbols, so the '>' and '<' orientation flips too in the output.
・4:B<-A
Incorrect. The builder is never truncated, so its length stays at 7 after "->C" was appended to the original four characters.
Explanation
StringBuilder is mutable, so append and reverse change route itself and return the same reference. Reversing "A->B->C" flips all seven characters, so both route and leg show the reversed sequence.
Example9 · Language Basics, Flow Control and Data Types
Consider this wrapper-caching snippet from a packet counter:
Integer a = 120;
Integer b = 120;
Integer c = 500;
Integer d = 500;
System.out.println((a == b) + "," + (c == d));
What does it print?
✓true,false
Correct. The Integer cache spans -128 to 127, so 120 reuses one object (true), while 500 produces separate objects (false).
・false,false
Incorrect. Values of 120 fall inside the cached Integer range, so a and b point to the identical cached object.
・true,true
Incorrect. Values of 500 lie beyond the cache, so c and d are distinct objects and == compares references, not equality.
・The program throws a NullPointerException during autoboxing of the values
Incorrect. No null is involved anywhere, so autoboxing of literal int values cannot raise a NullPointerException here.
Explanation
Integer.valueOf caches boxed values from -128 to 127, so two 120s share a reference and == is true. Because 500 is outside that range, each boxing creates a new object and == returns false.
Example10 · Language Basics, Flow Control and Data Types
A game backend tracks a player's score with this snippet:
int base = 10;
long bonus = 3_000_000_000L;
var total = base + bonus;
System.out.println(total);
What is the result?
✓3000000010
Correct. The int base is promoted to long, the sum stays within long range, and var infers long, so it prints 3000000010.
・Compilation fails because var cannot infer a type from a mixed int and long expression
Incorrect. var infers the type of the whole initializer expression, which is long after binary numeric promotion of base.
・Compilation fails because the underscore literal 3_000_000_000L is not a valid long literal
Incorrect. Underscores between digits are permitted and the L suffix makes it a valid long literal.
・Throws an ArithmeticException due to overflow when adding int to long at runtime
Incorrect. Java arithmetic silently wraps on overflow and never throws ArithmeticException for plain + on primitives.
Explanation
Binary numeric promotion widens the int operand to long before addition. var simply infers the static type of the initializer, which is long here, and the value is well within range.
Example11 · Language Basics, Flow Control and Data Types
A tenant provisioning service builds a slug:
String name = "Acme";
String a = name + "Corp";
String b = "AcmeCorp";
String c = name.concat("Corp").intern();
System.out.println((a == b) + " " + (c == b));
Assuming b is a compile-time constant held in the string pool, what prints?
・true true, because both expressions are folded into the pool at compile time
Incorrect. a is not a compile-time constant because name is a non-final variable, so the + yields a new runtime String outside the pool.
・false false, because intern never returns the existing pooled reference here
Incorrect. intern() deliberately returns the canonical pooled instance, so c==b is true rather than false here.
✓false true
Correct. a is built from a non-final variable so it is a new runtime object (a==b false), while intern() returns the pooled reference equal to b (c==b true).
・true false, since the runtime concatenation is pooled but intern is ignored
Incorrect. This reverses both results; the runtime concat stays unpooled while the interned reference does match the pool.
Explanation
String concatenation involving a non-final variable happens at runtime and yields a fresh object, so a==b is false. Calling intern() returns the pool's canonical reference, which equals the compile-time constant b, so c==b is true.
Example12 · Language Basics, Flow Control and Data Types
A clerk processes a birth-certificate fee in this snippet:
public class Registry {
public static void main(String[] args) {
int fee = 7;
int paid = 20;
System.out.println(paid / fee + " remainder " + paid % fee);
}
}
What is printed?
✓2 remainder 6
Correct. Integer division of 20 by 7 truncates toward zero giving 2, and the modulo operator 20 % 7 yields 6, so the concatenated output is '2 remainder 6'.
・2.857 remainder 6
Incorrect. Both operands are of type int, so no floating-point promotion occurs and the division never produces the fractional value 2.857 shown here.
・2 remainder 20
Incorrect. The modulo of 20 % 7 is 6, not 20; the remainder would only equal 20 if the divisor were larger than the dividend.
・3 remainder 6
Incorrect. Integer division truncates rather than rounds, so 20 / 7 evaluates to 2 instead of being rounded up to 3 as suggested.
Explanation
For int operands, / performs integer (truncating) division and % returns the remainder. 20 / 7 is 2 and 20 % 7 is 6, producing '2 remainder 6'.
Example13 · Language Basics, Flow Control and Data Types
A hospital roster builds a label with text blocks and string handling:
public class Roster {
public static void main(String[] args) {
var ward = """
Ward 3
Beds: 12""";
System.out.println(ward.lines().count());
}
}
What does this print?
・3
Incorrect. There is no blank trailing line because the closing delimiter sits immediately after '12', so the stream does not contain a third empty element.
・1
Incorrect. The block clearly contains two separate content lines separated by a line terminator, so the count cannot collapse down to a single line.
✓2
Correct. The text block has no trailing newline after 'Beds: 12', so it contains exactly two lines, and lines().count() returns 2.
・0
Incorrect. The string is non-empty and holds printable content, so lines() produces a non-empty stream rather than reporting zero lines.
Explanation
Incidental leading whitespace is stripped based on the least-indented line, and because the closing """ follows '12' with no newline, the block has two lines. String.lines() counts line terminators plus the final line, giving 2.
Example14 · Language Basics, Flow Control and Data Types
A quoting engine computes a premium adjustment:
public class Quote {
public static void main(String[] args) {
int base = 5;
int surcharge = base++ + ++base;
System.out.println(surcharge + " " + base);
}
}
What is printed?
・The program prints 11 7, treating base++ as the value 4.
Incorrect. This requires the first operand to be 4, but base++ yields the current value 5 before incrementing, not 4.
✓The program prints 12 7 on a single output line.
Correct. base++ evaluates to 5 (base becomes 6), then ++base pre-increments base to 7 and evaluates to 7; 5 + 7 is 12 and base ends at 7.
・The program prints 12 6, leaving the variable base at 6.
Incorrect. This assumes base ends at 6, but ++base raises it from 6 to 7 after base++ already moved it to 6.
・The program prints 10 6, applying one increment to base.
Incorrect. This treats both operations as one increment, yet post-increment and pre-increment each change base separately.
Explanation
Post-increment uses the value before incrementing, while pre-increment increments first and uses the new value. Starting at 5: base++ gives 5 (base->6), ++base gives 7 (base->7), summing to 12 with base at 7.
Example15 · Language Basics, Flow Control and Data Types
A booking service builds a confirmation message:
public class Booking {
public static void main(String[] args) {
String code = "SEAT";
code.concat("-12A");
StringBuilder sb = new StringBuilder("ROW");
sb.append("-9B");
System.out.println(code + " " + sb);
}
}
What is printed?
・The output is SEAT-12A ROW-9B, since concat mutated code.
Incorrect. This assumes concat mutated code, but String is immutable and concat returns a new discarded String, so code stays SEAT.
✓The output is SEAT ROW-9B on one line.
Correct. String.concat returns a new object that is ignored, leaving code as SEAT, while StringBuilder.append mutates sb in place to ROW-9B.
・The output is SEAT-12A ROW, since append returned a copy.
Incorrect. This assumes append returned a discarded copy, but append mutates the existing StringBuilder, so sb becomes ROW-9B.
・The output is SEAT ROW, since both calls were discarded.
Incorrect. This treats both calls as discarded, but append does mutate sb while only the concat result is actually lost.
Explanation
String is immutable, so concat produces a new String that is discarded when not assigned, leaving code unchanged. StringBuilder is mutable, so append modifies sb directly and it becomes ROW-9B.
▶ Try 10 free questions (graded)