Page 1 of 1
About Question enthuware.ocpjp.i.v11.2.3068 :
Posted: Thu Nov 07, 2019 6:57 pm
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
or would it be something else?
Re: About Question enthuware.ocpjp.i.v11.2.3068 :
Posted: Thu Nov 07, 2019 10:28 pm
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.
Re: About Question enthuware.ocpjp.i.v11.2.3068 :
Posted: Mon Oct 26, 2020 4:01 am
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));
Re: About Question enthuware.ocpjp.i.v11.2.3068 :
Posted: Mon Oct 26, 2020 5:31 am
by admin
You are right. It should be sort. Fixed.
thank you for your feedback!
Re: About Question enthuware.ocpjp.i.v11.2.3068 :
Posted: Wed Dec 02, 2020 2:00 pm
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") );
Re: About Question enthuware.ocpjp.i.v11.2.3068 :
Posted: Thu Dec 03, 2020 3:15 am
by admin
You are most likely using an older version of JDK. It compiles fine with JDK 11.