IT
Modern Java: Beyond Java 17 and Project Loom
Test your mastery of modern Java features including Virtual Threads, Sealed Classes, Pattern Matching, and JVM internals.
This is a free, 16-question multiple-choice quiz. Answer each question to see whether you got it right, with an explanation for every answer. There is no sign-up and no time limit — take it as many times as you like, and scroll down for the full answer key once you are done.
Question 1 of 16
0 correct
What happens when a Virtual Thread in Java executes a blocking I/O operation like reading from a SocketChannel?
Press A–D to choose · Enter to submit
Answer key & explanations
Every question in this quiz, with the correct answer marked and an explanation of why it is right. Use it to revise before or after taking the quiz above.
1.What happens when a Virtual Thread in Java executes a blocking I/O operation like reading from a SocketChannel?
- AThe underlying OS carrier thread is blocked until data arrives
- BThe virtual thread unmounts from its carrier thread, freeing it for other tasks✓ Correct
- CAn asynchronous callback is registered and an IOException is thrown
- DThe virtual thread is promoted to a standard platform thread
Correct answer: The virtual thread unmounts from its carrier thread, freeing it for other tasks
Project Loom virtual threads unmount from their carrier platform thread upon encountering standard blocking calls, allowing the carrier to execute other runnable virtual threads.
2.Which scenario causes 'pinning' of a Virtual Thread to its carrier thread in modern Java?
- ACalling Thread.sleep() for more than 1000 milliseconds
- BExecuting blocking file I/O inside a synchronized block or method✓ Correct
- CReading data across an encrypted TLS/SSL socket
- DAccessing a concurrent hash map from within a lambda expression
Correct answer: Executing blocking file I/O inside a synchronized block or method
When a virtual thread executes inside a synchronized block/method or native call (JNI), it is pinned to its carrier thread and cannot unmount during blocking operations.
3.What restriction applies to direct subclasses of a 'sealed' class in Java?
- AThey must be declared as final, sealed, or non-sealed✓ Correct
- BThey must reside within a completely different package
- CThey can only be defined as local anonymous classes
- DThey cannot implement any interfaces
Correct answer: They must be declared as final, sealed, or non-sealed
Every permitted subclass of a sealed class must explicitly declare a modifier to define its inheritance boundary: final, sealed, or non-sealed.
4.Which feature in modern Java enables decomposing a record instance into its constituent components within a switch statement?
- AType erasure destructuring
- BRecord patterns✓ Correct
- CDynamic component casting
- DReflection unpacking
Correct answer: Record patterns
Record patterns allow matching a record type and simultaneously extracting its component variables directly within switch cases and instanceof checks.
5.How does Java's Foreign Function and Memory (FFM) API improve upon JNI (Java Native Interface)?
- AIt converts native C code directly into JVM bytecode at runtime
- BIt offers pure Java bindings to off-heap memory and foreign functions without C glue code✓ Correct
- CIt restricts all native pointer operations strictly to garbage-collected heap spaces
- DIt automatically executes foreign C functions inside lightweight virtual threads
Correct answer: It offers pure Java bindings to off-heap memory and foreign functions without C glue code
The FFM API allows Java programs to allocate off-heap memory safely via Arena and link foreign native functions without authoring boilerplate C/C++ JNI wrappers.
6.What is the primary benefit of Structured Concurrency in Project Loom?
- AIt guarantees lock-free synchronization across CPU cores
- BIt treats concurrent tasks running in subthreads as a single unit of work with clean cancellation✓ Correct
- CIt converts multi-threaded loops into GPU compute kernels
- DIt forces all virtual threads to share a single global stack frame
Correct answer: It treats concurrent tasks running in subthreads as a single unit of work with clean cancellation
Structured Concurrency links related concurrent subtasks into a lexical scope, ensuring error propagation, short-circuit cancellation, and reliable cleanup.
7.Which garbage collector in OpenJDK provides ultra-low pause times that do not scale with the size of the active heap?
- ASerial GC
- BParallel GC
- CZGC✓ Correct
- DCMS GC
Correct answer: ZGC
ZGC performs almost all garbage collection work concurrently, keeping pause times typically under one millisecond regardless of whether the heap is gigabytes or terabytes in size.
8.What is the default accessibility level of compact constructors in Java records?
- AIt must match the accessibility level of the record class itself✓ Correct
- BIt must always be private to enforce immutability
- CIt defaults strictly to package-private
- DIt must be explicitly declared as protected
Correct answer: It must match the accessibility level of the record class itself
A compact constructor in a record implicitly has the same access modifier as the record class itself.
9.Which component manages off-heap native memory lifetimes in Java's Foreign Function & Memory API?
- AMemorySegmentCleaner
- BArena✓ Correct
- COffHeapManager
- DNativeReferenceQueue
Correct answer: Arena
An Arena controls the lifecycle of native memory segments, providing deterministic, bounded allocation scopes like automatic, global, or confined arenas.
10.What happens if a switch expression over a sealed interface hierarchy omits one of the permitted implementations without providing a default clause?
- AThe switch silently evaluates to null at runtime
- BThe compiler raises a compilation error due to inexhaustive patterns✓ Correct
- CA MatchFailureException is automatically thrown during class loading
- DThe missing case is executed as a no-op
Correct answer: The compiler raises a compilation error due to inexhaustive patterns
Pattern matching in switch expressions requires exhaustiveness; omitting a permitted subclass of a sealed hierarchy without a default branch results in a compile-time error.
11.How does Scoped Values (JEP 446/481) contrast with ThreadLocal variables when used with Virtual Threads?
- AScoped values are mutable and passed via deep copying
- BScoped values are immutable, bound to a call scope, and shareable efficiently across thousands of threads✓ Correct
- CScoped values consume heap memory proportional to thread stack depth
- DScoped values require registering a cleanup shutdown hook on the JVM
Correct answer: Scoped values are immutable, bound to a call scope, and shareable efficiently across thousands of threads
Scoped values provide lightweight, immutable data inheritance suited for millions of virtual threads, avoiding the unbound memory overhead of ThreadLocal.
12.What does the 'non-sealed' modifier explicitly indicate when applied to a subclass of a sealed class?
- AThe subclass cannot be instantiated directly
- BThe subclass re-opens the inheritance hierarchy so any other class can extend it✓ Correct
- CThe subclass must declare all its methods as final
- DThe subclass is converted into an abstract interface
Correct answer: The subclass re-opens the inheritance hierarchy so any other class can extend it
The 'non-sealed' modifier breaks the closed hierarchy, allowing unknown subclasses to extend it freely while preserving the sealed rules of its parent.
13.What is the main purpose of the Vector API introduced in recent OpenJDK incubator releases?
- ATo replace java.util.Vector with a thread-safe synchronized list
- BTo express vector computations that compile into SIMD instructions on supported hardware✓ Correct
- CTo calculate geometric 3D graphics in JavaFX applications
- DTo optimize pointer dereferencing inside the HotSpot compiler
Correct answer: To express vector computations that compile into SIMD instructions on supported hardware
The Vector API allows developers to express data-parallel computations directly in Java, generating optimal Single Instruction Multiple Data (SIMD) CPU instructions at runtime.
14.Which statement accurately describes String Templates as introduced in modern Java preview features?
- AThey perform compile-time macro expansion like C preprocessors
- BThey combine literal text with embedded expressions processed safely by template processors✓ Correct
- CThey strictly enforce SQL query parameter validation at the bytecode level
- DThey automatically translate strings into localized resource bundles
Correct answer: They combine literal text with embedded expressions processed safely by template processors
String Templates combine text with expressions via processors (like STR or RAW) to safely produce domain objects, JSON, or validated SQL.
15.In class file mechanics, what is the significance of the Compact Object Headers project (Project Lilliput)?
- AIt compresses the bytecode of loaded class files in metaspace
- BIt reduces the standard object header size from 128/96 bits down to 64 bits✓ Correct
- CIt packs primitive fields into 16-bit register blocks
- DIt eliminates the need for reference pointers on 64-bit systems
Correct answer: It reduces the standard object header size from 128/96 bits down to 64 bits
Project Lilliput optimizes object headers by shrinking the mark and class-pointer words into a single 64-bit word, significantly reducing heap footprints.
16.When using Pattern Matching for instanceof, what is the scope of the pattern variable?
- AIt is scoped only to the lines where the condition evaluates to true✓ Correct
- BIt is scoped globally to the entire enclosing method
- CIt remains accessible in the else branch as an uninitialized reference
- DIt is restricted to private fields of the enclosing class
Correct answer: It is scoped only to the lines where the condition evaluates to true
Flow typing governs pattern variables; they are only in scope where the matching condition is definitely true (flow scoping).
More free quizzes
- ITDistributed Systems: Consensus & Fault ToleranceDeep dive into distributed systems engineering, covering consensus protocols (Raft/Paxos), the CAP theorem, and vector clocks.16 questions
- ITLLM Engineering: Fine-Tuning, Alignment & ServingTest your knowledge of transformer architectures, PEFT techniques (LoRA), RLHF, vLLM, and production deployment strategies.16 questions
- ITApplied AI: Systems, Embeddings & RAGAssess your understanding of practical machine learning architectures, embedding spaces, and Retrieval-Augmented Generation systems.16 questions