Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Medium
Question 52
Given code of Test.java file:
- package com.udayan.ocp;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.stream.Stream;
- public class Test {
- public static void main(String[] args) throws IOException {
- Stream<Path> files = Files.list(Paths.get(System.getProperty("user.home")));
- files.forEach(System.out::println);
- }
- }
System.getProperty("user.home") returns the HOME directory of the User (Both in windows and Linux).
What will be the result of compiling and executing Test class?
- A It will only print the paths of directories and files under HOME directory.
- B It will only print the paths of files (not directories) under HOME directory.
- C It will print the paths of directories, sub-directories and files under HOME directory.
- D It will print the paths of files (not directories) under HOME directory and its sub-directories.
Reveal correct answer
Correct answer: A
Explanation
Files.list(Path) returns the object of Stream<Path> containing all the paths (files and directories) of current directory. It is not recursive.
For recursive access use overloaded Files.walk() methods.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
