J.VI · Java 25 practice question

What happens when the following Java 25 program runs on a newly created `ScopedValue`?

void main() {
    ScopedValue<Integer> tenant = ScopedValue.newInstance();
    System.out.println(tenant.get());
}
Choose one answer
Check answer and explanation

Correct answer: It throws `NoSuchElementException` because the scoped value is unbound.

Explanation

Because `ScopedValue.newInstance()` creates a scoped value that is initially unbound for all threads, calling `get()` before binding throws `NoSuchElementException`.

Common trap

`ScopedValue` does not default to `0` or `null` when unbound.

Source: Java SE 25 API: ScopedValue.newInstance
Checked against the cited text on 2026-11-27.

Report an error in this question