1Z0-829 Java I/O API and Securing Applications Practice Question
A developer needs to create a temporary file that will be automatically deleted when the JVM terminates. Which approach correctly achieves this?
⚠ Common exam trap
A common mix-up: candidates confuse creating a temporary file with simply scheduling deletion of any file, or they forget that `Files.createTempFile()` alone does not register for automatic deletion, leading them to pick option D without the necessary `.deleteOnExit()` call.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
File.createTempFile("pre", ".txt").deleteOnExit()
`File.createTempFile("pre", ".txt")` creates a temporary file in the default temporary-file directory, and chaining `.deleteOnExit()` registers that file for deletion when the JVM terminates. This combination ensures both the creation of a temporary file and its automatic cleanup upon JVM shutdown, fulfilling the requirement precisely.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
File.createTempFile("pre", ".txt").deleteOnExit()
Why this is correct
Creates a temp file and registers it for automatic deletion on JVM exit.
- ✗
new File("tmp.txt").deleteOnExit()
Why it's wrong here
Does not create a temporary file; only registers an existing file for deletion.
- ✗
Files.createTempDirectory("tmp").toFile().deleteOnExit()
Why it's wrong here
Creates a temporary directory, not a file.
- ✗
Files.createTempFile("pre", ".txt")
Why it's wrong here
Creates a temp file but does not register for deletion.
Go deeper
Related to this question
About these practice questions
One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.