This set of Java Multiple Choice Questions & Answers (MCQs) on “File and Directory”.
1. Which method is used to create a directory with fileattributes? Answer: c 2. Which method can be used to check fileAccessiblity? Answer: d 3. How can we delete all files in a directory? Answer: a 4. How to copy the file from one location to other? Answer: a 5. How can we get the size of specified file? Answer: b 6. How to read entire file in one line using java 8? Answer: a 7. How can we create a symbolic link to file? Answer: c 8. How can we filter lines based on content? Answer: a 9. Which jar provides FileUtils which contains methods for file operations? Answer: b 10. Which feature of java 7 allows to not explicitly close IO resource? Answer: c
a) Path.create()
b) Path.createDirectory()
c) Files.createDirectory(path, fileAttributes)
d) Files.create(fileAttributes)
Clarification: New directory can be created using Files.createDirectory(path, fileAttribute).
a) isReadable(path)
b) isWritable(path)
c) isExecutable(path)
d) isReadable(path), isWritable(path), and isExecutable(path)
Clarification: File accessibilty can be checked using isReadable(Path), isWritable(Path), and isExecutable(Path).
a) Files.delete(path)
b) Files.deleteDir()
c) Directory.delete()
d) Directory.delete(path)
Clarification: The delete(Path) method deletes the file or throws an exception if the deletion fails. If file does not exist a NoSuchFileException is thrown.
a) Files.copy(source, target)
b) Path.copy(source, target)
c) source.copy(target)
d) Files.createCopy(target)
Clarification: 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.
a) capacity(path)
b) size(path)
c) length(path)
d) Path.size()
Clarification: size(Path) returns the size of the specified file in bytes.
a) Files.readAllLines()
b) Files.read()
c) Files.readFile()
d) Files.lines()
Clarification: 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.
a) createLink()
b) createSymLink()
c) createSymbolicLink()
d) createTempLink()
Clarification: createSymbolicLink() creates a symbolic link to a target.
a) lines.filter()
b) filter(lines)
c) lines.contains(filter)
d) lines.select()
Clarification: lines.filter(line -> line.contains(“===—> Loaded package”)) can be used to filter out.
a) file
b) apache commons
c) file commons
d) dir
Clarification: FileUtils is a part of apache commons which provides various methods for file operations like writeStringToFile.
a) try catch finally
b) IOException
c) AutoCloseable
d) Streams
Clarification: Any class that has implemented Autocloseable releases the I/O resources.
