1Z0-830

Free 1Z0-830 practice questions

10 questions from our 1Z0-830 practice tests, each with the correct answer and a written explanation. Read them with the answers shown, or switch to quiz mode and answer first.

Back to the 1Z0-830 practice exams

Question 1 of 10, Handling Date, Time, Text, Numeric and Boolean Values

Given this code fragment: String s = """ Hello,\ World """; System.out.println(s.length()); What is printed?

  1. A14
  2. B18
  3. C12
  4. D13 Correct answer

Answer and explanation: The closing delimiter sets the incidental indentation to four spaces, so the second line keeps one leading space. The backslash at the end of the first line is a line-continuation escape, so the two lines are joined without a line terminator, giving "Hello, World" followed by the final newline: 13 characters. 12 forgets the trailing newline, 14 assumes the backslash still leaves a newline, and 18 ignores the stripping of incidental whitespace.

Question 2 of 10, Controlling Program Flow

Given: static String describe(Object o) { return switch (o) { case Integer i when i > 10 -> "big"; case Integer i -> "int"; case String s -> "str" + s.length(); default -> "other"; }; } What does System.out.println(describe(42) + describe(7) + describe("abc") + describe(3.0)); print?

  1. ACompilation fails because the guarded case dominates the unguarded one
  2. Bbigbigstr3other
  3. Cintintstr3other
  4. Dbigintstr3other Correct answer

Answer and explanation: Pattern labels are tested top to bottom: 42 matches the guarded Integer case, 7 fails the guard and falls to the unguarded Integer case, "abc" matches the String case, and 3.0 (a Double) reaches default. A guarded pattern does not dominate an unguarded pattern of the same type, so ordering it first is legal; it is the reverse order that fails to compile. The other outputs ignore the guard or apply it to every Integer.

Question 3 of 10, Using Object-Oriented Concepts in Java

Given: record Range(int lo, int hi) { Range { if (lo > hi) { int t = lo; lo = hi; hi = t; } } } What does System.out.println(new Range(5, 2)); print?

  1. ACompilation fails because compact constructor parameters are final
  2. BRange@ followed by a hash code, because records do not override toString
  3. CRange[lo=5, hi=2]
  4. DRange[lo=2, hi=5] Correct answer

Answer and explanation: In a compact constructor the parameters are ordinary, non-final variables, and their values at the end of the constructor body are assigned to the fields automatically, so the swap takes effect. The generated toString lists components in declaration order as Range[lo=2, hi=5]. Only the fields themselves cannot be assigned in a compact constructor, and records always get a generated toString.

Question 4 of 10, Using Object-Oriented Concepts in Java

Given: public final class Team { private final List<String> members; public Team(List<String> members) { this.members = members; } public List<String> members() { return members; } } Callers can still change a Team's members after construction. Which change makes Team immutable?

  1. AReturn Collections.unmodifiableList(members) from members() but keep storing the caller's list
  2. BDeclare the members field volatile so every thread sees the same list contents
  3. CStore List.copyOf(members) in the constructor and return that field from members() Correct answer
  4. DMake the constructor private and add a static factory method that takes the same list

Answer and explanation: The caller keeps a reference to the list passed in, so a defensive copy is needed on the way in; List.copyOf creates an unmodifiable copy that can also be returned safely. An unmodifiable view returned from the accessor still reflects changes the original caller makes to its own list. volatile only affects visibility, and a factory method that stores the same list has the same flaw.

Question 5 of 10, Handling Exceptions

Given: static int depth = 0; static void recurse() { depth++; recurse(); } and in main: try { recurse(); } catch (Exception e) { System.out.println("exception"); } catch (Error e) { System.out.println("error " + (depth > 0)); } What is printed?

  1. Aexception
  2. Berror true Correct answer
  3. Cerror false
  4. DNothing, because a StackOverflowError can never be caught

Answer and explanation: Unbounded recursion eventually throws StackOverflowError, which is a subclass of Error, not Exception, so the first catch clause does not match. Errors can be caught like any Throwable, although recovering is usually unwise; after the stack unwinds there is room to print. depth was incremented on every call, so it is greater than zero.

Question 6 of 10, Working with Arrays and Collections

Given (Java 21): var set = new LinkedHashSet<>(List.of("b", "c")); set.addFirst("a"); set.addLast("b"); System.out.println(set + " " + set.reversed().getFirst()); What is printed?

  1. A[a, c, b] b Correct answer
  2. BAn UnsupportedOperationException is thrown by addFirst
  3. C[a, b, c, b] b
  4. D[a, b, c] c

Answer and explanation: LinkedHashSet implements SequencedSet in Java 21. addFirst and addLast insert the element at the requested end, and if it is already present they relocate it there, so b moves to the end and the set becomes [a, c, b]. A set never holds duplicates, and reversed() returns a reverse-ordered view whose first element is b. Only unmodifiable or sorted sequenced collections reject addFirst.

Question 7 of 10, Working with Streams and Lambda Expressions

Given: Stream.of("a", "b", "c", "d") .peek(System.out::print) .map(String::toUpperCase) .limit(2) .forEach(System.out::print); What is printed?

  1. AaAbBcCdD
  2. BabcdAB
  3. CaAbB Correct answer
  4. DabAB

Answer and explanation: Streams are lazy and process one element at a time through the whole pipeline, so each element is peeked and then printed in upper case before the next one is pulled. limit(2) is a short-circuiting operation, so after two elements no more are pulled and c and d are never peeked. Printing all peeks first would require a stateful barrier such as sorted().

Question 8 of 10, Packaging and Deploying Java Code

The JAR file commons-text-1.10.0.jar has no module-info.class and its manifest has no Automatic-Module-Name attribute. When the JAR is placed on the module path, which name must other modules use to require it?

  1. Acommons.text Correct answer
  2. Bcommons
  3. CNone, because a JAR without a module descriptor cannot be required by name
  4. Dcommons.text.1.10.0

Answer and explanation: A JAR on the module path without a descriptor becomes an automatic module. Without an Automatic-Module-Name attribute its name is derived from the file name: the .jar extension and the trailing version are dropped and non-alphanumeric separators become dots, giving commons.text. The version is never part of the name, the whole stem is kept rather than only its first part, and automatic modules can be required like any named module.

Question 9 of 10, Managing Concurrent Code Execution

Given: class Counter { private static int total; private int mine; static synchronized void addTotal() { total++; } synchronized void addMine() { mine++; } } Thread T1 is currently executing Counter.addTotal(). Thread T2 calls addMine() on a Counter instance c. Which statement is true?

  1. AT2 can run addMine() at the same time, because the two methods lock different monitors Correct answer
  2. BT2 blocks until T1 leaves addTotal(), because all synchronized methods of a class share one lock
  3. CThe code fails to compile because a class cannot mix static and instance synchronized methods
  4. DT2 blocks only if c was the instance most recently used by T1

Answer and explanation: A static synchronized method locks the Class object (Counter.class), whereas an instance synchronized method locks the instance it is called on. Because the monitors are different, the two methods do not exclude each other. Mixing both kinds in one class is legal; to protect shared static state from instance methods, they must synchronize on the same lock explicitly.

Question 10 of 10, Using Java I/O API

Given: Path p = Path.of("/var/log/app/server.log"); System.out.println(p.startsWith("/var/log") + " " + p.startsWith("/var/lo") + " " + p.endsWith("server.log") + " " + p.endsWith(".log") + " " + p.subpath(1, 3)); On a Unix-like file system, what is printed?

  1. Atrue false true false var/log
  2. Btrue false true false log/app Correct answer
  3. Ctrue false true true log/app
  4. Dtrue true true true log/app

Answer and explanation: Path.startsWith and endsWith compare whole name elements, not characters, so "/var/lo" and ".log" do not match even though the string would. subpath(1, 3) uses zero-based name indexes, excluding the root: index 1 is log and index 2 is app, and the result is a relative path.