About Question enthuware.ocpjp.v8.2.1572 :

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

Moderator: admin

Post Reply
edmuns
Posts: 3
Joined: Fri Feb 02, 2018 11:06 am
Contact:

About Question enthuware.ocpjp.v8.2.1572 :

Post by edmuns »

Hi!

For my Ubuntu machine i've got this output:
c:\main\project\Starter.java

And there is no such answer or mentions about OS in the question.


However for this code from the book I've appropriate behavior of getName():

Code: Select all

Path path = Paths.get("/land/hippo/harry.happy");
System.out.println("The Path Name is: "+path);
for(int i=0; i<path.getNameCount(); i++) {
System.out.println("
Element "+i+" is: "+path.getName(i));
}
The Path Name is: /land/hippo/harry.happy
Element 0 is: land
Element 1 is: hippo
Element 2 is: harry.happy


Looks like my environment can't understand "\\", it works fine with "/" though. I'm using 1.8.0 openjdk-amd64.

Am I missing something or the test contains a mistake?

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

Re: About Question enthuware.ocpjp.v8.2.1572 :

Post by admin »

First thing is that you need to use the official Oracle's JDK while preparing fo the exam.
Second, if you see a path starting with c:\\, you can safely assume that it is a windows path and if the path contains / (i.e. forward slash), you can safely assume that it is a *nix path. So you test the code accordingly.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

edmuns
Posts: 3
Joined: Fri Feb 02, 2018 11:06 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1572 :

Post by edmuns »

For latest oracle jdk 8_162 I got same results.

Code: Select all

System.out.println(Paths.get("C:\\main\\project\\Starter.java").getName(0).toString());
Output:

c:\main\project\Starter.java

edmuns
Posts: 3
Joined: Fri Feb 02, 2018 11:06 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1572 :

Post by edmuns »

I've used some online compilers and the result is same. Example:

https://codebunk.com/b/364194431/

Or you can try at your own this code:

Code: Select all

import java.nio.file.Path;
import java.nio.file.Paths;
class Main {
    public static void main(String[] args) {
             System.out.println(Paths.get("C:\\main\\project\\Starter.java").getName(0));
    }
}

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

Re: About Question enthuware.ocpjp.v8.2.1572 :

Post by admin »

If you run it on *nix, then what you are getting is correct because backslash and colon are not path separators on *nix. Therefore, the whole name is considered as one single component.

That is why I mentioned the second point to you i.e. if you see a path starting with c:\\, you can safely assume that it is a windows path. In this case, it is a windows path and so if you run it on windows, you will get "main".

In the exam, you need to select the best option and option 5 i.e. main is the best option here.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 51 guests