Java OCA OCP Practice Question 3132

Question

Consider the following program:

import java.io.IOException;
import java.nio.file.*;

public class Matcher {
        public static void main(String []args) {
               Path currPath = Paths.get(".");
               try (DirectoryStream<Path> stream =
                       Files.newDirectoryStream(currPath, "*o*?{java,class}")) {
                       for(Path file : stream) {
                               System.out.print(file.getFileName() + " ");
                       }// w  w w.  ja v  a 2 s  . c  om
               } catch (IOException ioe) {
                       System.err.println("An I/O error occurred... exiting ");
               }
        }
}

Assume that the current path in which the program is run has the following files:

Copy.class, /*  w ww . j  ava2s . c  o m*/
Copy.java, 
Dir.class, 
Dir.java, 
Hello.class, 
hello.html, 
Matcher.class, 
Matcher.java, 
OddEven.class, and  
PhotoCopy.java.

Assuming that the program ran without throwing IOException.

Which one of the following options correctly describes the behavior of this program when it is executed?

a) Prints the following: Copy.class Copy.java Hello.class hello.html OddEven.class PhotoCopy.java
b) Prints the following: Copy.class Copy.java PhotoCopy.java
c) Prints the following: Hello.class hello.html OddEven.class PhotoCopy.java
d) Prints the following: Copy.class Copy.java Hello.class OddEven.class PhotoCopy.java
e) Prints the following: PhotoCopy.java
f) Does not print any output in console
g) Throws the exception java.util.regex.PatternSyntaxException because the pattern is invalid.


32.

Note

In the Glob pattern "o?{java,class,html}", the character * matches any number of characters, so o matches any string that has "o" in it.

The ? matches exactly one character.

The pattern {java,class} matches files with the suffixes of "java" or "class".

Hence, from the given files, the matching file names are

Copy.class, 
Copy.java, 
Hello.class, 
OddEven.class, 
PhotoCopy.java.



PreviousNext

Related