About Question enthuware.ocpjp.v17.2.3675 :

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

Moderator: admin

Post Reply
Badem48
Posts: 26
Joined: Thu Aug 24, 2023 4:33 pm
Contact:

About Question enthuware.ocpjp.v17.2.3675 :

Post by Badem48 »

Hi,

I'd like to point out that this question seems straightforward but actually touches on an important issue.

Consider what happens if s1 is defined like this:

Code: Select all

String s1 = """
             a \
             b \t
             c \s
             efg""";
Or would there be any difference with this variation?

Code: Select all

String s1 = """
             a \
             b \t
             c \s
             efg
             """;
In this context, I have no questions but in the explanation there's a bit of confusion as to why s1.split("\\n").length is 2 instead of 3, even though there appear to be 3 lines. Could the last element of the array be an empty string, perhaps? Maybe that can be added to the explanation.

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

Re: About Question enthuware.ocpjp.v17.2.3675 :

Post by admin »

Good point. The explanation has been enhanced to include this point:
Finally, this string is being split using the new line character. String's split method returns an array of Strings. But the split method does not include trailing empty strings in the resulting array. That is why, although the string pointed to by s1 ends with a new line and thus contains 3 lines, the split method returns only 2 strings - "a b \t" and "c \s". Thus, s1.split("\\n").length returns 2.
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 249 guests