🎙 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 6 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("x.txt");
        Files.writeString(p, "ab\n");
        Files.writeString(p, "cd\n", StandardOpenOption.APPEND);
        System.out.println(Files.size(p) + " " + Files.readAllLines(p).size());
    }
}
  1. A.6 2
  2. B.3 1
  3. C.6 1
  4. D.4 2
Answer & explanation

Correct answer: 6 2

Each write is 3 bytes (two letters and \n), so the file has 6 bytes and 2 lines.

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 →