OpenExamsFREE PRACTICE — ANY EXAM

Free Java SE 21 Developer Professional Practice Questions

200 free questions · Answer explanations · No signup required

View examination details, outline and sources

Loading selected examination banks…

Sample Practice Questions

Try these 10 sample questions from the Java SE 21 Developer Professional practice bank. Each includes the answer and explanation. Continue with the interactive practice above to review the full question set.

  1. 1What happens when the following Java SE 21 code executes? `Integer value = null; int result = value;`

    1. A.It throws a NullPointerException.Correct answer: A
    2. B.It assigns zero to result.
    3. C.It fails to compile because unboxing is forbidden.
    4. D.It throws an ArithmeticException.

    Explanation: The Java Language Specification states: "If r is null, unboxing conversion throws a NullPointerException". Assigning the wrapper to an int invokes unboxing, so execution throws that exception.

    Reference: Java Language Specification, Java SE 21 Edition: Conversions and Contexts — §5.1.8, Unboxing Conversion

  2. 2A colon-style switch may fall through. What text does this particular Java code print? `int n = 1; switch (n) { case 1: System.out.print("A"); case 2: System.out.print("B"); break; default: System.out.print("C"); }`

    1. A.A
    2. B.ABCorrect answer: B
    3. C.ABC
    4. D.B

    Explanation: The specification states: "all the statements in the switch block that follow the switch label are executed in order" The first group falls through to the second, whose break exits the switch.

    Reference: Java Language Specification, Java SE 21 Edition: Blocks, Statements, and Patterns — §14.11.3, Execution of a switch Statement

  3. 3Two references are assigned as `Object a = new Object(); Object b = a;`. What does the identity comparison `a == b` evaluate to?

    1. A.true because both references denote the same objectCorrect answer: A
    2. B.false because the variables have different names
    3. C.false because assigning b creates another object
    4. D.true because all Object instances compare equal

    Explanation: The specification states: "At run time, the result of == is true if the operand values are both null or both refer to the same object or array" Assignment copies the reference, so both variables refer to the same object.

    Reference: Java Language Specification, Java SE 21 Edition: Expressions — §15.21.3, Reference Equality Operators

  4. 4`class Failure extends Exception {}` defines a checked exception. A method throws a new Failure and does not catch it. What must its declaration do?

    1. A.Declare Failure or a superclass in its throws clause.Correct answer: A
    2. B.Declare only RuntimeException in its throws clause.
    3. C.Declare no throws clause because custom exceptions are unchecked.
    4. D.Return Failure instead of throwing it.

    Explanation: The specification states: "It is a compile-time error if a method or constructor body can throw some exception class E when E is a checked exception class and E is not a subclass of some class declared in the throws clause of the method or constructor." An uncaught checked Failure must be covered by the declared throws type.

    Reference: Java Language Specification, Java SE 21 Edition: Exceptions — §11.2.3, Exception Checking

  5. 5An `ArrayList<String>` initially contains `[A, B, C]`. What does it contain after `list.add(1, "X")`?

    1. A.[A, X, B, C]Correct answer: A
    2. B.[A, X, C]
    3. C.[X, A, B, C]
    4. D.[A, B, X, C]

    Explanation: The API states: "Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices)." The new element is inserted at index 1 and B and C shift right.

    Reference: Java SE 21 API Specification: List — add(int, E)

  6. 6What list results from `Stream.of(1, 2, 3, 4).filter(n -> n % 2 == 0).toList()`?

    1. A.[1, 3]
    2. B.[2, 4]Correct answer: B
    3. C.[1, 2, 3, 4]
    4. D.[4, 2]

    Explanation: The API states: "Returns a stream consisting of the elements of this stream that match the given predicate." The predicate retains the even values in encounter order.

    Reference: Java SE 21 API Specification: Interface Stream — filter(Predicate)

  7. 7Which directive declares that the current module depends on `com.example.lib`?

    1. A.requires com.example.lib;Correct answer: A
    2. B.exports com.example.lib;
    3. C.uses com.example.lib;
    4. D.opens com.example.lib;

    Explanation: The specification states: "The requires directive specifies the name of a module on which the current module has a dependence." The other directives operate on packages or services.

    Reference: Java Language Specification, Java SE 21 Edition: Packages and Modules — §7.7.1, Dependences

  8. 8Which Java SE 21 call creates and schedules a virtual thread to execute Runnable task?

    1. A.Thread.startVirtualThread(task)Correct answer: A
    2. B.Thread.ofVirtual().unstarted(task)
    3. C.Thread.ofPlatform().start(task)
    4. D.task.run()

    Explanation: The API states: "Creates a virtual thread to execute a task and schedules it to execute." startVirtualThread both creates and schedules the virtual thread.

    Reference: Java SE 21 API Specification: Class Thread — startVirtualThread(Runnable)

  9. 9What does `InputStream.read()` return when the end of the stream has been reached?

    1. A.-1Correct answer: A
    2. B.0
    3. C.255
    4. D.256

    Explanation: The API states: "If no byte is available because the end of the stream has been reached, the value -1 is returned." The sentinel -1 is distinct from byte values 0 through 255.

    Reference: Java SE 21 API Specification: Class InputStream — read()

  10. 10Which call produces a Locale with language `fr` and region `CA`?

    1. A.Locale.forLanguageTag("fr-CA")Correct answer: A
    2. B.Locale.of("fr-CA")
    3. C.Locale.forLanguageTag("fr")
    4. D.Locale.of("ca", "FR")

    Explanation: The API states: "Returns a locale for the specified IETF BCP 47 language tag string." forLanguageTag accepts the language tag.

    Reference: Java SE 21 API Specification: Locale — forLanguageTag(String)

Topics in this practice bank

These topics describe the available practice for Java SE 21 Developer Professional. Review the examination outline and its sources for scope and allocation details.

  • Handling Date, Time, Text, Numeric and Boolean Values
    • Primitives, wrappers, expressions, Math API and conversions
    • Text, text blocks, String and StringBuilder
    • Date-Time API, zones and daylight saving time
  • Controlling Program Flow
    • Conditional, switch and loop constructs; break and continue
  • Using Object-Oriented Concepts in Java
    • Objects, nested classes, references and garbage collection
    • Classes, records, fields, methods, constructors and initializers
    • Method overloading and variable arguments
    • Variable scope, encapsulation, immutability and local type inference
    • Inheritance, sealed types, polymorphism, casting and pattern matching
    • Interfaces, functional interfaces and interface methods
    • Enums with fields, methods and constructors
  • Handling Exceptions
    • Try/catch/finally, resources, multi-catch and custom exceptions
  • Working with Arrays and Collections
    • Arrays, List, Set, Map and Deque operations
  • Working with Streams and Lambda expressions
    • Object and primitive stream pipelines with functional interfaces
    • Decomposition, concatenation, reduction, grouping and partitioning
  • Packaging and Deploying Java Code
    • Modules, reflection, dependencies, services, providers and consumers
    • Compilation, JARs, runtime images and module migration
  • Managing Concurrent Code Execution
    • Platform and virtual threads, tasks, lifecycle and executors
    • Thread safety, locking and concurrent APIs
    • Concurrent collection processing and parallel streams
  • Using Java I/O API
    • Console and file I/O streams
    • Object serialization and deserialization
    • Path objects, properties and java.nio.file
  • Implementing Localization
    • Locales, resource bundles, message and value parsing and formatting

How to use these free practice questions

Choose an answer, check the explanation, and review your progress by chapter. Practice questions and explanations are free to access without signing up.

These are independent study questions, not actual examination items. For examination requirements and official information, view Java SE 21 Developer Professional details.