PASS - TAKER : Anonymous

Java SE 8 OCP - IO and NIO

Correct : 18

100% Complete (success)

100 %

False : 0

0% Complete (success)

0 %

Anonymous 2020-01-10T23:07:47


2. Given: {code} and that the invocation java Directories file2.txt is issued from a directory that has two subdirectories, "dir1" and "dir2," and that "dir1" has a file "file1.txt" and "dir2" has a file "file2.txt," and the output is "false true," which set(s) of code fragments must be inserted? (Choose all that apply.)

Because you are invoking the program from the directory whose direct subdirectories are to be searched, you don't start your path with a File.separator character. The exists()method tests for either files or directories; the isFile() method tests only for files. Since we're looking for a file, both methods work.


3. Given: {code} And given that myfile.txt contains the following two lines of data: ab cd What is the result?

You need to call flush()only when you're writing data. Readers don't have flush() methods. If not for the call to flush(), answer C would be correct.


4. Given: {code} If line 6 creates a valid Console object and if the user enters fred as a username and 1234 as a password, what is the result? (Choose all that apply.)

The readPassword() method returns a char[]. If a char[] were used, answer B would be correct.


5.Given: {code} Instances of which class(es) can be serialized? (Choose all that apply.)

Dodge instances cannot be serialized because they "have" an instance of Wheels, which is not serializable. Vehicle instances cannot be serialized even though the subclass Car can be.


6. Which of the following creates a Path object pointing to c:/temp/exam? (Choose all that apply.)

E and F are correct since Paths must be created using the Paths.get() method. This method takes a varargs String parameter, so you can pass as many path segments to it as you like. A and B are incorrect because you cannot construct a Path directly. C and D are incorrect because the Files class works with Path objects but does not create them from Strings.


7. Given a directory tree at the root of the C: drive and the fact that no other files exist: dir x - | ..........| - dir y ..........| - file a and these two paths: Path one = Paths.get("c:/x"); Path two = Paths.get("c:/x/y/a"); Which of the following statements prints out: y/a ?

A is correct because it prints the path to get to two from one. B is incorrect because it prints out ../.. which is the path to navigate to one from two. This is the reverse of what we want. C, D, and E are incorrect because it does not make sense to call resolve with absolute paths. They might print out c:/x/c:/x/y/a, c:/x/y/a/c:/x, and c:/x/y/a/c:/x/y/a, respectively. F is incorrect because of the above. Note that the directory structure provided is redundant. Neither relativize() nor resolve() requires either path to actually exist.


8. Given the following statements: I. A nonempty directory can usually be deleted using Files.delete II. A nonempty directory can usually be moved using Files.move III. A nonempty directory can usually be copied using Files.copy Which of the following is true?

E is correct because a directory containing files or subdirectories is copied or moved in its entirety. Directories can only be deleted if they are empty. Trying to delete a nonempty directory will throw a DirectoryNotEmptyException. The question says "usually" because copy and move success depends on file permissions. Think about the most common cases when encountering words such as "usually" on the exam.


9. Given: new File("c:/temp/test.txt").delete(); How would you write this line of code using Java 7 APIs?

B is correct because, like the Java 7 code, it returns false if the file does not exist. A is incorrect because this code throws an Exception if the file does not exist. C, D, E, and F are incorrect because they do not compile. There is no deleteOnExit() method, and file operations such as delete occur using the Files class rather than the path object directly.


10. Given: {code} Which code inserted at // CODE HERE will compile and run without error on Windows? (Choose all that apply.)

A, B, and D are correct. Creation time is a basic attribute, which means you can read BasicFileAttributes or any of its subclasses to read it. DosFileAttributes is one such subclass. C is incorrect because you cannot cast a more general type to a more specific type. E is incorrect because this example specifies it is being run on Windows. While it would work on UNIX, it throws an UnsupportedOperationException on Windows due to requesting the WindowsFileSystemProvider to get a POSIX class. F and G are incorrect because those methods do not exist. You must use the Files class to get the attributes.


11. Which of the following are true? (Choose all that apply.)

BasicFileAttributes is the general superclass. DosFileAttributes subclasses BasicFileAttributes for Windows operating systems. PosixFileAttributes subclasses BasicFileAttributes for UNIX/Linux/Mac operating systems.


12. Given a partial directory tree: dir x - | ..........| - dir y ..........| - file a In what order can the following methods be called if walking the directory tree from x? (Choose all that apply.) I: preVisitDirectory x II: preVisitDirectory x/y III: postVisitDirectory x/y IV: postVisitDirectory x V: visitFile x/a

B and C are correct because file visitor does a depth-first search. When files and directories are at the same level of the file tree, they can be visited in either order. Therefore, "y" and "a" could be reversed. All of the subdirectories and files are visited before postVisit is called on the directory.


13. Given: {code} Which code inserted at // CODE HERE would cause the FileVisitor to stop visiting files after it sees the file

E is correct because it is the correct constant to end the FileVisitor. B is incorrect because END is not defined as a result constant. A, C, and D are incorrect. While they are valid constants, they do not end file visiting. CONTINUE proceeds as if nothing special has happened. SKIP_SUBTREE skips the subdirectory, which doesn't even make sense for a Java file. SKIP_SIBLINGS would skip any files in the same directory. Since we weren't told what the file structure is, we can't assume there aren't other directories or subdirectories. Therefore, we have to choose the most general answer of TERMINATE. F is incorrect because file visitor throws a NullPointerException if null is returned as the result.


14. Assume all the files referenced by these paths exist: {code} What is the correct string to pass to PathMatcher to match both these files?

B is correct. ** matches zero or more characters, including multiple directories. A is incorrect because */ only matches one directory. It will match "temp" but not "c:/temp," let alone "c:/temp/dir." C is incorrect because *.txt only matches filenames and not directory paths. D, E, and F are incorrect because the paths we want to match do not begin with a slash. G is incorrect because of the above.


15. Given a partial directory tree at the root of the drive: dir x - | ..........| = file a.txt ..........| - dir y ....................| - file b.txt ....................| - dir y ..............................| - file c.txt And the following snippet: {code} What is the result?

C is correct because DirectoryStream only looks at files in the immediate directory. **/*.txt means zero or more directories followed by a slash, followed by zero or more characters followed by .txt. Since the slash is in there, it is required to match, which makes it mean one or more directories. However, this is impossible because DirectoryStream only looks at one directory. If the expression were simply *.txt, answer A would be correct.


16. Given a partial directory tree: dir x - | ..........| - dir y ..........| -file a and given that a valid Path object, dir, points to x, and given this snippet: WatchKey key = dir.register(watcher, ENTRY_CREATE); If a WatchService is set using the given WatchKey, what would be the result if a file is added to dir y?

A is correct because watch service only looks at a single directory. If you want to look at subdirectories, you need to set recursive watch keys. This is usually done using a FileVisitor.


17. Given: {code} What is the result?

It’s okay for a class to implement Serializable even if its superclass doesn’t. However, when you deserialize such an object, the nonserializable superclass must run its constructor. Remember, constructors don’t run on deserialized classes that implement Serializable.


18. Given: {code} What is the result? (Choose all that apply.)

An instance of type Computer Has-a Keyboard. Because Keyboard doesn’t implement Serializable, any attempt to serialize an instance of Computer will cause an exception to be thrown. If Keyboard did implement Serializable, then two objects would have been serialized.


19. Given: {code} Which are true? (Choose all that apply.)

C is correct because static and transient variables are not serialized when an object is serialized. F is a valid statement. G is incorrect because you don’t implement the defaultReadObject() method; you call it from within the readObject() method, along with any custom read operations your class needs.