Page 1 of 1

About Question enthuware.ocpjp.v11.2.3336 :

Posted: Sun Jan 31, 2021 8:38 pm
by OliviaJohnson
Are you sure that Option D is incorrect?
It will print an exception stack trace the file referred to by p cannot be deleted due to lack of appropriate permissions.
It will not throw any exception in that case. It will just return false.
I have this code and it does throw an Exception

Code: Select all

    public static void main(String[] args) throws IOException {
        SecurityManager sm1 = new SecurityManager();
        System.setSecurityManager(sm1);

        Path p = Path.of("C:\\ioPractice\\deleteTest");

        try{
            var b = Files.deleteIfExists(p);
            System.out.println(b);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

Code: Select all

Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\ioPractice\deleteTest" "delete")
My .java.policy file content that causes the Exception. I commented out the line with the permission

Code: Select all

grant {
  //permission java.io.FilePermission "c:\\ioPractice\\deleteTest", "delete";
};
My .java.policy file content that does not cause the Exception. I un-comment the permission line. Now the code returns true

Code: Select all

grant {
  permission java.io.FilePermission "c:\\ioPractice\\deleteTest", "delete";
};
The code won't throw an Exception if SecurityManager is not enabled, but the question did not let the reader know whether the SecurityManager was enabled or not.

Re: About Question enthuware.ocpjp.v11.2.3336 :

Posted: Mon Feb 01, 2021 1:04 am
by admin
Actually, it is talking about file permissions. If the Security Manager was supposed to be enabled, the question would have mentioned that. But you are right, one could get an impression that the question is talking about permissions administered by the security manager.

For the purpose of the exam, you should not assume that Security Manager is being used unless specified explicitly.

HTH,
Paul.