This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “File and Directory”.
1. Which method is used to create a directory with fileattributes?
a) Path.create()
b) Path.createDirectory()
c) Files.createDirectory(path, fileAttributes)
d) Files.create(fileAttributes)
View Answer
Explanation: New directory can be created using Files.createDirectory(path, fileAttribute).
2. Which method can be used to check fileAccessiblity?
a) isReadable(path)
b) isWritable(path)
c) isExecutable(path)
d) isReadable(path), isWritable(path), and isExecutable(path)
View Answer
Explanation: File accessibilty can be checked using isReadable(Path), isWritable(Path), and isExecutable(Path).
3. How can we delete all files in a directory?
a) Files.delete(path)
b) Files.deleteDir()
c) Directory.delete()
d) Directory.delete(path)
View Answer
Explanation: The delete(Path) method deletes the file or throws an exception if the deletion fails. If file does not exist a NoSuchFileException is thrown.
4. How to copy the file from one location to other?
a) Files.copy(source, target)
b) Path.copy(source, target)
c) source.copy(target)
d) Files.createCopy(target)
View Answer
Explanation: Files.copy(source, target) is used to copy a file from one location to another. There are various options available like REPLACE_EXISTING, COPY_ATTRIBUTES and NOFOLLOW_LINKS.
5. How can we get the size of specified file?
a) capacity(path)
b) size(path)
c) length(path)
d) Path.size()
View Answer
Explanation: size(Path) returns the size of the specified file in bytes.
6. How to read entire file in one line using java 8?
a) Files.readAllLines()
b) Files.read()
c) Files.readFile()
d) Files.lines()
View Answer
Explanation: Java 8 provides Files.readAllLines() which allows us to read entire file in one task. We do not need to worry about readers and writers.
7. How can we create a symbolic link to file?
a) createLink()
b) createSymLink()
c) createSymbolicLink()
d) createTempLink()
View Answer
Explanation: createSymbolicLink() creates a symbolic link to a target.
8. How can we filter lines based on content?
a) lines.filter()
b) filter(lines)
c) lines.contains(filter)
d) lines.select()
View Answer
Explanation: lines.filter(line -> line.contains(“===—> Loaded package”)) can be used to filter out.
9. Which jar provides FileUtils which contains methods for file operations?
a) file
b) apache commons
c) file commons
d) dir
View Answer
Explanation: FileUtils is a part of apache commons which provides various methods for file operations like writeStringToFile.
10. Which feature of java 7 allows to not explicitly close IO resource?
a) try catch finally
b) IOException
c) AutoCloseable
d) Streams
View Answer
Explanation: Any class that has implemented Autocloseable releases the I/O resources.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice BCA MCQs
- Practice Information Technology MCQs
- Check Java Books
- Apply for Java Internship
- Check Programming Books