250+ TOP MCQs on File and Directory and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) 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)

Answer: c
Clarification: 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)

Answer: d
Clarification: 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)

Answer: a
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.

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)

Answer: a
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.

5. How can we get the size of specified file?
a) capacity(path)
b) size(path)
c) length(path)
d) Path.size()

Answer: b
Clarification: 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()

Answer: a
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.

  250+ TOP MCQs on Creating Threads and Answers

7. How can we create a symbolic link to file?
a) createLink()
b) createSymLink()
c) createSymbolicLink()
d) createTempLink()

Answer: c
Clarification: 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()

Answer: a
Clarification: 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

Answer: b
Clarification: 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

Answer: c
Clarification: Any class that has implemented Autocloseable releases the I/O resources.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top