Here are 10 essential multiple-choice questions on Java File Handling, covering key concepts.
Question 1
Which package contains the File class in Java?
java.io.file
java.file
java.io
java.nio.file
Question 2
What is the purpose of the createNewFile() method in the File class?
To open an existing file
To create a new directory
To create a new file if it does not exist
To delete a file
Question 3
What will file.exists() return if the file doesn’t exist?
true
false
null
Throws Exception
Question 4
Which of the following can read text from a file line-by-line in Java?
FileWriter
BufferedWriter
Scanner
PrintWriter
Question 5
What is the output of this code?
File f = new File("demo.txt");
System.out.println(f.isDirectory());
Assume demo.txt is a file, not a folder.
true
false
null
Compilation Error
Question 6
Which method is used to get the size of a file in bytes?
getSize()
getLength()
length()
size()
Question 7
What does delete() method of the File class return when deletion fails?
Throws an exception
Returns false
Returns null
Returns the file name
Question 8
What is the correct way to create a directory in Java?
File.mkdir()
File.createDirectory()
File.createDir()
File.makeDirectory()
Question 9
Which Java class is best suited for writing text to a file?
Scanner
FileReader
BufferedWriter
InputStreamReader
Question 10
What exception must be handled when performing file operations in Java?
IOException
InterruptedException
SQLException
ArrayIndexOutOfBoundsException
There are 10 questions to complete.