• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Question about question Q 8-7 (Mala Gupta Programmer 2)

 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question:

Q 8-7. Which options are true for the following code?

a check() will output "Not exists" and try to create a new file, if it doesn’t exist.
b check() will output "exists" and try to delete the file if it exists. It won’t delete a directory.
c check() can never output "can never reach here".
d check() can throw an IOException, NoSuchFileException.


answer:

A 8-7. a, d
[8.2] Check, delete, copy, or move a file or directory with the Files class
Explanation: Option (b) is incorrect because Files.delete() can be used to delete
files and empty directories. If you try to delete a nonempty directory, Files.delete()
will throw a DirectoryNotEmptyException.
Option (c) is incorrect because both exists() and notExists() can return false
if the underlying system can’t determine the existence of a file.


Why question c is not correct (i attach picture) also i don`t get why NoSuchFileException may be throw because i try delete file that checked to exists before?
question.png
[Thumbnail for question.png]
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Files.exists()

javadoc wrote:Note that the result of this method is immediately outdated. If this method indicates the file exists then there is no guarantee that a subsequence access will succeed. Care should be taken when using this method in security sensitive applications.


javadoc wrote:Returns:
true if the file exists; false if the file does not exist or its existence cannot be determined.



There are the same notes for notExists().

I hope this piece of documentation answers your question.
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Note that the result of this method is immediately outdated

this sentence help.
As i understand that after checking other process can create/delete file or directory and this lead to exception.
also this not mentioned (concurrent access) that make answer to question hardly. but i think i get it
 
Author
Posts: 375
22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sergej -

Thanks for posting your query and concern.

The answer to this question is correct. But, I'll add to the explanation of this question.



Let's mark a few lines of code to enable me to refer to them -




This question is tricky. First, the conditions at lines marked with comment 'line 1' and 'line 2' use operator !, which makes the code a bit difficult to comprehend ('if Files.exists(fileName)' is simpler to understand than 'if !Files.exists(fileName)').

The second tricky part comes from the usage of the word 'never' in option (c), that is, 'check() can never output "can never reach here"'. If any one condition can result in output of this text, this option won't be considered correct.

The third tricky part comes from the behavior of the Files.exists() and Files.notExists() methods. I'm quoting Java documentation for both these methods -

Files.exists() - Note that the result of this method is immediately outdated. If this method indicates the file exists then there is no guarantee that a subsequence access will succeed. Care should be taken when using this method in security sensitive applications.

Files.notExists() - As with the exists method, the result of this method is immediately outdated. If this method indicates the file does exist then there is no guarantee that a subsequence attempt to create the file will succeed. Care should be taken when using this method in security sensitive applications.

Note that this method is not the complement of the exists method. Where it is not possible to determine if a file exists or not then both methods return false.




Now, lets work with a condition, which 'can' result in the output of text 'can never reach here' -

STEP1 : Imagine Files.exists(path) returns true for file referred by value of path, at line 1. Because the condition uses the negation operator, the 'if' block won't execute and the control is transferred to if-else at line 2.

STEP2: Because the result of Files.exists(path) is immediately outdated, it is possible that Files.notExists(path) returns true for file referred by path at line 2 (the file could have been deleted since code on line 1 executed). Again, because the condistion at line 2 also uses the negation operator, the 'if-else' block won't execute and the control is transferred to else block. Execution of else block will result in output 'can never reach here'.




Option (d) is correct because it states that the code can throw IOException, NoSuchFileException.

Option (d) doesn't list out the conditions when the code might throw these exceptions. It just states the probability of these exceptions being thrown by the code.

The code might throw IOException due to an I/O error, when it tries to execute Files.createDirectories(), Files.createFile(path) or Files.delete(path). It might throw NoSuchFileException, a checked exception, when it makes an attempt to execute Files.delete(path), trying to access the file referred by path. As mentioned above, results of the method notExists() is immediately outdated.



Thanks.

With much respect,
Mala
 
It's never done THAT before. Explain it to me tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic