← Quiz: File Handling in Java: all 10 questions
Quiz: File Handling in Java · Question 2 of 10
What does this print? (code)
What does this print?
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
Path p = Path.of("a.txt");
Files.writeString(p, "one\n");
Files.writeString(p, "two\n");
System.out.println(Files.readAllLines(p));
}
}
- A.[one, two]
- B.[one]
- C.[two]
- D.[]
Answer & explanation
Correct answer: [two]
writeString replaces the file by default, so only the second write remains. Use StandardOpenOption.APPEND to add.
Can you get the rest right without peeking?
Take the timed Quiz: File Handling in Java quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →