About Question enthuware.ocpjp.v11.2.3336 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OliviaJohnson
Posts: 7
Joined: Sat Jan 30, 2021 2:04 pm
Contact:

About Question enthuware.ocpjp.v11.2.3336 :

Post 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.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 8 guests