🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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));
    }
}
  1. A.[one, two]
  2. B.[one]
  3. C.[two]
  4. 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 →