J.VI · Java 25 practice question
What is printed when this Java 25 program runs?
import java.util.concurrent.CompletableFuture;
class Demo {
public static void main(String[] args) {
CompletableFuture<String> cf = new CompletableFuture<>();
boolean first = cf.complete("ready");
boolean second = cf.complete("override");
boolean canceled = cf.cancel(true);
System.out.println(first + ":" + second + ":" + canceled + ":" + cf.join());
}
}
Check answer and explanation
Correct answer: true:false:false:ready
Explanation
Only the first completion attempt on a CompletableFuture succeeds; subsequent calls to complete or cancel return false and leave the initial value ready intact.
Common trap
Returning true:true:false:override is tempting if a developer thinks complete overwrites an existing result like AtomicReference.set, but a CompletableFuture can be completed only once. Returning true:false:true:ready is tempting if someone assumes cancel can always abort a future even after completion, yet cancel also fails once completed.
Source: Java SE 25 API: CompletableFuture
Checked against the cited text on 2026-11-25.