Which statement about sealed classes is correct?
Sealed classes use the 'permits' keyword to explicitly declare which classes are allowed to extend them. This provides fine-grained control over the class hierarchy.
Oracle Certification
59 practice questions with correct answers and detailed explanations. Use this guide to review concepts before taking the practice exam.
The Oracle Java SE Developer (1Z0-819) certification validates professional expertise in Oracle technologies. This study guide covers all 59 practice questions from our 1Z0-819 practice test, complete with correct answers and explanations to help you understand each concept thoroughly.
Review each question and explanation below, then test yourself with the full interactive practice exam to measure your readiness.
Which statement about sealed classes is correct?
Sealed classes use the 'permits' keyword to explicitly declare which classes are allowed to extend them. This provides fine-grained control over the class hierarchy.
What is the output of the following code? var list = List.of(1, 2, 3); list.stream().map(x -> x * 2).forEach(System.out::println);
The stream maps each element to its double value (1→2, 2→4, 3→6), then forEach prints each result on a new line using the println method reference.
Which of the following is a valid use of the var keyword in Java 11+?
The var keyword requires an initializer to infer the type. Option C shows a valid declaration where the type is inferred as List<String>. Options A and D lack proper initialization, and var cannot be used as a return type.
What happens when you call notifyAll() on a monitor object without holding its lock?
The notifyAll() method requires the calling thread to hold the monitor's lock. Calling it without the lock throws IllegalMonitorStateException at runtime.
Which functional interface is used by the Comparator.comparing() method?
Comparator.comparing() takes a Function that extracts a comparable key from an object. The Function interface takes a type parameter and returns a comparable result.
In the try-with-resources statement, what must a resource implement to be automatically closed?
Resources in try-with-resources must implement either Closeable or AutoCloseable. The JVM automatically calls close() on these objects when exiting the try block.
What is the type of the stream operation result in this code? Stream<Integer> stream = Stream.of(1, 2, 3); stream.filter(x -> x > 1).collect(Collectors.toSet());
The collect() method with Collectors.toSet() returns a Set. The filter() operation is intermediate and doesn't change the final type determined by the collector.
Which statement correctly describes record constructors in Java 16+?
Records can define custom constructors as long as they maintain the record's component contract. You can create a compact constructor or full constructor with the same parameters as components.
What does the following code print? String s = "hello"; s = s.concat(" world"); System.out.println(s);
The concat() method returns a new String object with the concatenated value. The assignment s = updates the reference to point to this new String.
Which of these correctly implements method overloading?
Method overloading requires different parameter lists (number or type of parameters). Option B has different numbers of parameters. Option A fails because return type alone doesn't distinguish overloads.
In a Stream pipeline, which of the following is an intermediate operation?
The peek() operation is intermediate—it returns a Stream and doesn't trigger evaluation. Operations like count(), forEachOrdered(), and findFirst() are terminal operations that produce final results.
What is the result of this operation? var map = new HashMap<String, Integer>(); map.put("a", 1); map.putIfAbsent("a", 2); System.out.println(map.get("a"));
putIfAbsent() only adds the key-value pair if the key is absent. Since "a" already maps to 1, the putIfAbsent call has no effect, leaving the value as 1.
Which statement about instanceof with pattern matching is true?
With pattern matching, the type variable is introduced by the pattern and scoped to the truthy branch of the instanceof check, eliminating the need for explicit casting within that scope.
What does the flatMap() operation do in a Stream context?
flatMap() applies a function that returns a Stream to each element, then concatenates all those streams into a single flattened stream, useful for handling nested collections.
Which of the following correctly uses method references with streams?
System.out::println is a valid method reference that works with forEach. Option A fails due to type mismatch (Integer has no length method), and option C has incorrect method reference syntax.
What is the output of this code? try { int result = 10 / 0; } catch (Exception e) { System.out.println("Error"); } finally { System.out.println("Finally"); }
The ArithmeticException (10/0) is caught by the Exception handler, printing 'Error'. The finally block always executes after catch, printing 'Finally' on a new line.
Which statement about module dependencies is correct?
The 'requires transitive' directive in module declarations implies that dependencies are re-exported. This allows transitive dependencies to be used by downstream modules.
What does the following collector do? stream.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
groupingBy with Function.identity() groups each element to itself, and the counting() collector counts how many times each element appears, producing a frequency map.
Which is true about LocalDateTime in java.time?
LocalDateTime is immutable and represents date and time without timezone. For timezone-aware datetimes, use ZonedDateTime or OffsetDateTime.
What is the result of this functional composition? Function<Integer, Integer> f = x -> x * 2; Function<Integer, Integer> g = x -> x + 3; var result = f.andThen(g).apply(5);
andThen() chains functions: first apply f (5 * 2 = 10), then apply g (10 + 3 = 13). Wait, that's 13. Let me recalculate: f.apply(5) = 10, then g.apply(10) = 13. The answer should be 13, but let me verify the composition order—andThen means f then g, so 5→10→13.
Which declaration correctly uses bounded type parameters in a generic method?
Option A correctly uses 'extends' for an upper bound on a type parameter, restricting T to Number and its subtypes. Java uses 'extends' for both class and interface bounds; 'super' is for wildcards, not type parameters.
What happens when you use the reduce() operation on an empty stream without a default value?
The reduce() method without an initial value returns Optional.empty() for empty streams. Using reduce(identity, accumulator, combiner) with an identity would return that identity.
Which statement about annotation retention policies is correct?
RetentionPolicy.CLASS (the default) includes annotations in the compiled class file but makes them unavailable to the reflection API at runtime. RetentionPolicy.RUNTIME keeps them available via reflection.
What does this code snippet demonstrate? var supplier = () -> new String("test"); var result = supplier.get();
The lambda () -> new String("test") implements Supplier<String>. The get() method is called to obtain the supplied String, demonstrating lazy initialization.
In terms of exception handling, which statement is accurate?
Resources in try-with-resources are closed in LIFO order (reverse declaration order). Options A and B describe inheritance issues—catching Exception before a more specific exception is legal but illogical. Option D is misleading about throws' purpose.
Which of the following correctly declares a local variable using the var keyword in Java 10+?
The var keyword requires explicit initialization to infer the type. Option B lacks initialization, C cannot infer type from null, and D works but A is the simplest valid example.
What is the result of executing the following code? String s = "Java"; s.concat(" Programming"); System.out.println(s);
Strings are immutable in Java. The concat() method returns a new String but does not modify the original. Since the result is not assigned, s remains "Java".
Which interface should be implemented to define a natural ordering for objects of a custom class?
Comparable defines a single natural ordering via compareTo(), while Comparator defines custom comparison logic. Cloneable and Serializable serve different purposes.
What will be printed when the following code executes? int[] nums = {1, 2, 3}; for (int num : nums) { if (num == 2) continue; System.out.print(num); }
The continue statement skips the current iteration. When num equals 2, the print statement is skipped, so only 1 and 3 are printed.
Which of the following statements about sealed classes is true?
Sealed classes (Java 15+) use the permits keyword to explicitly list which classes may extend them. Option C is partially correct but not entirely accurate as subclasses can be in different files if properly accessible.
What is the output of the following code? List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.remove(Integer.valueOf(2)); System.out.println(list);
Integer.valueOf(2) creates an Integer object equal to 2. The remove() method removes the first element equal to that value, leaving [1, 3].
Which method from the Stream API would you use to filter elements that match a specific condition?
filter() accepts a Predicate and keeps only elements that satisfy the condition. map() transforms elements, reduce() combines them, and forEach() is a terminal operation.
What will happen when you compile and run this code? public class Test { public static void main(String[] args) { Integer a = 127; Integer b = 127; System.out.println(a == b); } }
Due to Integer caching, values between -128 and 127 are cached and reused. Both a and b reference the same cached object, so == returns true.
Which of the following correctly implements the try-with-resources statement for automatic resource management?
Try-with-resources requires parentheses after try, and the resource must implement AutoCloseable. The resource is automatically closed after the block.
What is the result of the following expression? String result = "5" + 3 + 2; int num = Integer.parseInt(result);
String concatenation occurs left-to-right: "5" + 3 = "53", then "53" + 2 = "532". Parsing "532" succeeds and returns 532, not option A. Option D is incorrect; the correct answer should be 532.
Which functional interface is used to define operations that consume a single argument and return no result?
Consumer<T> takes one argument and returns void (accepts and performs side effects). Function returns a result, Supplier takes no arguments, and Predicate returns boolean.
What will be printed when the following code executes? Map<String, Integer> map = new HashMap<>(); map.put("a", 1); map.put("b", 2); map.putIfAbsent("a", 10); System.out.println(map.get("a"));
putIfAbsent() only inserts if the key is absent. Since "a" already exists with value 1, the insertion is skipped and the original value 1 remains.
Which of the following correctly uses method references in Java?
Method references use :: syntax without parentheses. Option B is missing ::, C has unnecessary parentheses after println, and D is a lambda expression (also valid but not a method reference).
What is the most appropriate way to handle checked exceptions when using the Stream API?
Since functional interfaces don't declare checked exceptions, the common approach is to wrap operations in try-catch within the lambda or create a wrapper method that converts checked to unchecked exceptions.
What output is produced by this code? String[] arr = {"A", "B", "C"}; List<String> list = Arrays.asList(arr); list.add("D"); System.out.println(list.size());
Arrays.asList() returns a fixed-size list backed by the array. Attempting to add elements throws UnsupportedOperationException since the list size cannot change.
Which statement about record classes in Java 14+ is correct?
Records automatically generate canonical methods and accessors. They are implicitly final, can implement interfaces, and can have custom constructors alongside the compact constructor.
Which of the following correctly demonstrates the use of a generic method?
Generic type parameters come before the return type. Option B is incorrect syntax, C is missing diamond operator danger, and D uses invalid 'where' clause (Java doesn't use this syntax).
What will happen when you execute this code? try { int result = 10 / 0; } catch (ArithmeticException | NullPointerException e) { System.out.println("Caught"); } finally { System.out.println("Finally"); }
Division by zero throws ArithmeticException, which is caught by the multi-catch block. The finally block always executes, so both "Caught" and "Finally" are printed.
Which of the following is the most efficient way to concatenate multiple strings in a loop?
StringBuilder is mutable and efficient for concatenation in loops. Option A creates many intermediate String objects, C is only for known strings, and D uses immutable concat() method.
What is the output of this code snippet? List<String> list = List.of("a", "b", "c"); list.stream() .map(String::toUpperCase) .peek(System.out::println) .count(); System.out.println("Done");
peek() is an intermediate operation (lazy), but count() is a terminal operation that forces evaluation. peek() prints each element on a new line due to println, then "Done" is printed.
Which of the following correctly implements the Comparable interface?
Option A uses generic Comparable correctly with proper type safety. Option B uses raw type, C incorrectly extends (should implement), and D uses Comparator instead of Comparable.
What does the following code demonstrate about variable scope? if (true) { int x = 5; } // System.out.println(x); // Line X int x = 10; System.out.println(x);
The x declared in the if block has block scope and is inaccessible outside. The second declaration at method scope is valid and prints 10. If line X uncommented, it would be a compilation error.
Which of the following correctly uses pattern matching in switch expressions (Java 16+)?
Option A correctly uses pattern matching with switch expressions. Option B uses old switch statement syntax, C has unnecessary parentheses, and D uses incorrect 'matches' keyword.
What is printed by the following code? List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); numbers.stream() .filter(n -> n % 2 == 0) .map(n -> n * n) .forEach(System.out::print);
filter() keeps only even numbers (2, 4). map() squares them (4, 16). forEach prints them without spaces: "416" but the answer is "49" based on numbers 2->4 and 4->16, so output is "416".
Which of the following statements about sealed classes is correct?
Sealed classes require the 'sealed' keyword and a 'permits' clause listing exactly which classes can extend them, providing fine-grained control over inheritance.
What is the output of the following code? var list = Arrays.asList(1, 2, 3, 4, 5); list.stream() .filter(n -> n % 2 == 0) .map(n -> n * 2) .reduce(0, Integer::sum); System.out.println(result);
The stream filters even numbers (2, 4), maps them to (4, 8), and reduces with sum starting at 0, resulting in 4 + 8 = 12.
Which of the following accurately describes the behavior of the 'var' keyword with diamond operators in Java?
The var keyword works seamlessly with diamond operators; the compiler infers the complete generic type from the constructor call and initialization context.
Which interface should be implemented to create a custom collector for use with the Stream API?
The Collector interface defines how elements from a stream are accumulated into a final result, with methods for supplier, accumulator, combiner, and finisher.
What will be the result of executing this code? String text = "Java"; String result = switch(text) { case "Java" -> "Object-Oriented"; case "Python" -> "Dynamic"; default -> "Unknown"; }; System.out.println(result);
Switch expressions with arrow syntax return the value immediately when the case matches; 'Java' matches the first case, returning 'Object-Oriented'.
Which of the following statements about record components and canonical constructors is correct?
When a canonical constructor is not explicitly defined, Java automatically generates one that assigns each parameter to its corresponding record component field.
Which of the following best describes how the Optional class helps prevent NullPointerException in Java?
Optional wraps potentially null values and provides functional methods for safe value access and transformation without direct null checks.
Given this code snippet, what is the most significant issue with the following pattern? try { // resource-dependent code } catch (Exception e) { throw new RuntimeException(e); } Instead of using try-with-resources or proper exception handling:
Without try-with-resources, resources aren't automatically closed, and wrapping exceptions in RuntimeException can mask critical context about what actually failed.
Which of the following is a valid use of pattern matching in Java 19+?
Pattern matching with instanceof combines the type check and casting in one expression, with the pattern variable in scope only if the condition is true.
In the context of Java module system, which statement correctly describes module readability and accessibility?
True accessibility requires two conditions: the requiring module must have 'requires' and the exporting module must have 'exports' for that specific package.
You've reviewed all 59 questions. Take the interactive practice exam to simulate the real test environment.
▶ Start Practice Exam — Free