About Question enthuware.ocpjp.i.v11.2.3068 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
DazedTurtle
Posts: 26
Joined: Wed Oct 02, 2019 1:42 pm
Contact:

About Question enthuware.ocpjp.i.v11.2.3068 :

Post by DazedTurtle »

With option 5, the "toArray()" method doesn't work because it returns an Object array, and we're trying to assign it to a String array, right?

As for the "<T> T[] toArray(T[] a)" method, would that have worked if option 5 had instead been written

Code: Select all

String[] sa;
values.toArray(sa);
or would it be something else?

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

Re: About Question enthuware.ocpjp.i.v11.2.3068 :

Post by admin »

As per List API for the <T> T[] toArray​(T[] a) method:
If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
and
Throws:
ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
NullPointerException - if the specified array is null
Thus, String[] sa; values.toArray(sa); will throw a NPE.

You can do this:
String[] sa = values.toArray(new String[0]); //valid, sa will be assigned a new array of Strings containing all the elements of the array pointed to by values.

Also, you should get into the habit of writing and executing test programs to see what happens. It is an important activity in exam preparation.

kabanvau
Posts: 14
Joined: Thu Nov 21, 2019 5:48 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3068 :

Post by kabanvau »

Option 1:
The lambda expresion is syntactically incorrect. If you want to specify the type of the lambda variables or if there are more than one arguments, then you have to put the argument list within parentheses, like this: values.forEach((var a, var b)->a.compareTo(b));


it will not compile:

values.forEach((var a, var b)->a.compareTo(b));

It should be values.sort((var a, var b)->a.compareTo(b));

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

Re: About Question enthuware.ocpjp.i.v11.2.3068 :

Post by admin »

You are right. It should be sort. Fixed.
thank you for your feedback!

MM987MM
Posts: 3
Joined: Wed Dec 02, 2020 1:53 pm
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3068 :

Post by MM987MM »

Option 4.
values.removeIf((var k)->k.equals("a") );
is marked as a correct answer.

It shouldn't be correct, because there is "var" which makes the compilation impossible.
The correct answer should be:
values.removeIf((k)->k.equals("a") );

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

Re: About Question enthuware.ocpjp.i.v11.2.3068 :

Post by admin »

You are most likely using an older version of JDK. It compiles fine with JDK 11.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests