Java OCA OCP Practice Question 359

Question

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. No notice is given
  • B. A notice related to dir x is issued
  • C. A notice related to dir y is issued
  • D. Notices for both dir x and dir y are given
  • E. An Exception is thrown
  • F. The behavior depends on the underlying operating system


A is correct because watch service only looks at a single directory.

Note

If you want to look at subdirectories, you need to set recursive watch keys.

This is usually done using a FileVisitor.




PreviousNext

Related