J.VI · Java 25 practice question

What does the following Java 25 program print when using `ScopedValue.orElse` on an unbound `ScopedValue`?

void main() {
    ScopedValue<String> REGION = ScopedValue.newInstance();
    System.out.println(REGION.orElse("us-east"));
}
Choose one answer
Check answer and explanation

Correct answer: us-east

Explanation

Because `ScopedValue.newInstance()` starts unbound for all threads, `orElse("us-east")` returns the fallback `"us-east"`.

Common trap

`orElse` avoids `NoSuchElementException` by returning the provided fallback instead of `null` or throwing.

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

Report an error in this question