Switch to Dark Mode

Java File Handling

Here are 10 essential multiple-choice questions on Java File Handling, covering key concepts.

Last Updated : Apr 4, 2025
Discuss
Comments

Question 1

Which package contains the File class in Java?

  • A

    java.io.file

  • B

    java.file

  • C

    java.io

  • D

    java.nio.file

Question 2

What is the purpose of the createNewFile() method in the File class?

  • A

    To open an existing file

  • B

    To create a new directory

  • C

    To create a new file if it does not exist

  • D

    To delete a file

Question 3

What will file.exists() return if the file doesn’t exist?

  • A

    true

  • B

    false

  • C

    null

  • D

    Throws Exception

Question 4

Which of the following can read text from a file line-by-line in Java?

  • A

    FileWriter

  • B

    BufferedWriter

  • C

    Scanner

  • D

    PrintWriter

Question 5

What is the output of this code?

Java
File f = new File("demo.txt");
System.out.println(f.isDirectory());

Assume demo.txt is a file, not a folder.

  • A

    true

  • B

    false

  • C

    null

  • D

    Compilation Error

Question 6

Which method is used to get the size of a file in bytes?

  • A

    getSize()

  • B

    getLength()

  • C

    length()

  • D

    size()

Question 7

What does delete() method of the File class return when deletion fails?

  • A

    Throws an exception

  • B

    Returns false

  • C

    Returns null

  • D

    Returns the file name

Question 8

What is the correct way to create a directory in Java?

  • A

    File.mkdir()

  • B

    File.createDirectory()

  • C

    File.createDir()

  • D

    File.makeDirectory()

Question 9

Which Java class is best suited for writing text to a file?

  • A

    Scanner

  • B

    FileReader

  • C

    BufferedWriter

  • D

    InputStreamReader

Question 10

What exception must be handled when performing file operations in Java?

  • A

    IOException

  • B

    InterruptedException

  • C

    SQLException

  • D

    ArrayIndexOutOfBoundsException

There are 10 questions to complete.

Take a part in the ongoing discussion