About Question enthuware.ocpjp.v11.2.1822 :

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

Moderator: admin

Post Reply
mccar_
Posts: 5
Joined: Sun Apr 04, 2021 2:19 pm
Contact:

About Question enthuware.ocpjp.v11.2.1822 :

Post by mccar_ »

Hi! It looks like this code print "1 1 1 1". I double checked. I try this on online compiler (google first result "java online compiler") to eliminate my JDK influence.

mccar_
Posts: 5
Joined: Sun Apr 04, 2021 2:19 pm
Contact:

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

Post by mccar_ »

JDK 1.8.0.211 return same result: "1 1 1 1"

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

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

Post by admin »

Code: Select all

class TestClass{
  public static void main(String args[]) throws Exception{
Path p1 = Paths.get("photos\\..\\beaches\\.\\calangute\\a.txt");
Path p2 = p1.normalize();
Path p3 = p1.relativize(p2);
Path p4 = p2.relativize(p1);

System.out.println(
    p1.getNameCount()+" "+p2.getNameCount()+" "+
    p3.getNameCount()+" "+p4.getNameCount());
  }
}
Prints 6 3 1 1 on Java 11 and 16

Print 6 3 9 9 on Java 8

Please post the exact code that you are trying out.
If you like our products and services, please help us by posting your review here.

mccar_
Posts: 5
Joined: Sun Apr 04, 2021 2:19 pm
Contact:

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

Post by mccar_ »

class TestClass{
public static void main(String args[]) throws Exception{
Path p1 = Paths.get("photos\\..\\beaches\\.\\calangute\\a.txt");
Path p2 = p1.normalize();
Path p3 = p1.relativize(p2);
Path p4 = p2.relativize(p1);

System.out.println(
p1.getNameCount()+" "+p2.getNameCount()+" "+
p3.getNameCount()+" "+p4.getNameCount());
}
}

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

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

Post by admin »

Not sure why you are getting 1 1 1 1. I am getting the output as mentioned above. Are you sure you have the exact same code and running it from command line?
If you like our products and services, please help us by posting your review here.

mccar_
Posts: 5
Joined: Sun Apr 04, 2021 2:19 pm
Contact:

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

Post by mccar_ »

I run your code in online compiler https://www.w3schools.com/java/tryjava. ... o_compiler and others to make suer that it is not my JDK issue. All results the same: 1 1 1 1.
To reproduce, please, copy code from your post into any online compiler and run it.
Online compilers list: https://www.baeldung.com/java-online-compilers

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

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

Post by admin »

oh, that's because the online compiler is using a unix based OS and on such OSs the path separator is /. Therefore, if you use "photos\\..\\beaches\\.\\calangute\\a.txt" on a unix based OS, it will be considered as a single name.

If you change the given path to "photos/../beaches/./calangute/a.txt" on Unix you will get the same result as given.

In the exam, if you see double backslash (\\), assume that it is a windows path and if you see a forward slash (/), assume that it is a unix path.

We use windows path because most people use Windows OS and so, windows path makes the code easier for them to test.
If you like our products and services, please help us by posting your review here.

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

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

Post by admin »

Also, we do not recommend using online compilers to try out code because you won't know which jdk build is being used. Better to use official Oracle or OpenJDK on your machine through command line.
If you like our products and services, please help us by posting your review here.

mccar_
Posts: 5
Joined: Sun Apr 04, 2021 2:19 pm
Contact:

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

Post by mccar_ »

Thank you! I use MacOS and that's why I see incorrect answer. With separator "/" method return correct answer.

61d14837
Posts: 13
Joined: Tue Jul 21, 2020 1:39 pm
Contact:

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

Post by 61d14837 »

Can you help explain why getNameCount() returns 1 instead of 0 when the Path is empty?

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

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

Post by admin »

That is how they have designed the Path class. Documentation for getNameCount says: "the number of elements in the path, or 0 if this path only represents a root component". So, the number of elements starts with 1, unless it is a root such as "c:\" on windows or "/" on unix.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest