Java OCA OCP Practice Question 2934

Question

Assuming that we have access to a WatchService object,

which has been properly initialized and registered with at least one Path, what two problems make the code unusable?

1: for(;;) { 
2:    WatchKey key = watchService.poll(); 
3:    for (WatchEvent<?> event: key.pollEvents()) 
4:    System.out.println(event.kind()+","+event.context()); 
5: } 
  • A. It does not check if the WatchKey is null.
  • B. It does not use the factory pattern.
  • C. The event type is not checked for OVERFLOW.
  • D. It does not cast WatchEvent<?> to WatchEvent<Path>.
  • E. It does not reset the WatchKey after use.
  • F. It uses an infinite loop that never ends.


A,E.

Note

First off, the WatchService.poll() method returns immediately with a value of null if there are no events available.

Unless the file system is constantly busy, this code is likely to produce a NullPointerException on line 3 almost immediately after it starts, rendering the code unusable; therefore A is correct.

The other major problem with this code is that the WatchKey method reset() is not called after the WatchKey is processed, meaning that this code can receive at most one event, at which point it loops infinitely; therefore E is correct.

B is irrelevant information and therefore incorrect.

For event type, it is recommended that you check for OVERFLOW events but it is not required, so C is incorrect.

The event details can be outputted without casting to WatchEvent<Path>, so D is incorrect.

Infinite loops are common with the WatchService API, so F is incorrect.




PreviousNext

Related