🎙 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 9 of 10

What does this print? (code)

What does this print?

import java.io.*;
import java.nio.file.*;
public class Main {
    public static void main(String[] args) {
        try {
            Files.delete(Path.of("nothing-here.txt"));
            System.out.println("deleted");
        } catch (NoSuchFileException e) {
            System.out.println("no such file");
        } catch (IOException e) {
            System.out.println("io error");
        }
    }
}
  1. A.deleted
  2. B.io error
  3. C.Nothing; delete ignores missing files
  4. D.no such file
Answer & explanation

Correct answer: no such file

Files.delete throws NoSuchFileException for a missing file. Files.deleteIfExists would return false instead.

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 →