Java Questions & Answers – File and Directory

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

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

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

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

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

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

Answer: b
Explanation: size(Path) returns the size of the specified file in bytes.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

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

Answer: c
Explanation: createSymbolicLink() creates a symbolic link to a target.
advertisement

8. How can we filter lines based on content?
a) lines.filter()
b) filter(lines)
c) lines.contains(filter)
d) lines.select()
View Answer

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

Answer: b
Explanation: FileUtils is a part of apache commons which provides various methods for file operations like writeStringToFile.
advertisement

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

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.