Java SE 8 Programmer II — Question 104
Given the code fragment:
Stream<Path> files = Files.list(Paths.get(System.getProperty("user.home"))); files.forEach (fName -> { //line n1 try {
Path aPath = fName.toAbsolutePath(); //line n2
System.out.println(fName + ":"
+ Files.readAttributes(aPath, Basic.File.Attributes.class).creationTime
());
} catch (IOException ex) {
ex.printStackTrace();
});
What is the result?
Answer options
- A. All files and directories under the home directory are listed along with their attributes.
- B. A compilation error occurs at line n1.
- C. The files and folders in the home directory are listed along with their attributes.
- D. A compilation error occurs at line n2.
Correct answer: C
Explanation
The correct answer is C because the code successfully retrieves and prints the attributes of the files and directories in the home directory without any compilation errors. Option A is incorrect because it implies a listing of all files and directories, while the code specifically focuses on the home directory. Options B and D are wrong because there are no compilation errors in either line n1 or n2.