About Question enthuware.ocajp.i.v8.2.1469 :

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

Moderator: admin

Post Reply
klhlubek
Posts: 7
Joined: Mon Oct 12, 2015 9:58 am
Contact:

About Question enthuware.ocajp.i.v8.2.1469 :

Post by klhlubek »

I think I have found a little ( It is very little) error. Though many people make that I would say it is clearly an error, so I would please you to correct it. Here is what I mean:
Syntactically, this lambda expression is correct. However, remember that a lambda expression does not create a new scope for variables. Therefore, you cannot reuse the variable names that have already been used to define new variables in your argument list .
Here, observe that the variable d is already defined so your argument list cannot use d as a variable name. It would be like defining the same variable twice in the same scope.
That is the first explanation of the first question and there is written about an argument list ( it is bold in the quoted test ), but what actually is meant is parameter list, because:

An argument list is a list of arguments, when you define for example a method.
A parameter list is a list of paramters and you call with a parameter list something like for example a method, but it has to fit with the argument list of the calles method.

So there is a difference between argument and parameter. ( This difference is for me very important, but I do not know how for you ).


There is a another thing about it. I would say it could be seen as correct, because you "define" the argument list for the method, which is later called ( it is a lambda expression ), but I would like it if you would add something to your explanation to make it more clear.

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

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Post by admin »

Yes, there is a difference between an argument and a parameter as you highlighted.
This explanation, however, is talking from a perspective of the call

Code: Select all

filterData(al, d -> d.value%2 == 0 );
.
Here, al and the lambda expression are actually arguments to the filter data method and not parameters.

I agree with your point in general, unfortunately, if you use the word parameter here, it will be confusing from the other perspective. Lambda expressions are peculiar in this respect because there is no clear distinction between an argument and a parameter. You actually specify arguments but the compiler creates a method definition out of it with parameters.

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

Lunarkiran
Posts: 4
Joined: Mon Feb 26, 2018 3:41 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Post by Lunarkiran »

Code: Select all

 public class Data {
	int value;

	Data(int value) {
		this.value = value;
	}

	public String toString() {
		return "" + value;
	}

	

	public static void main(String[] args) {

	ArrayList<Data> al=new ArrayList<>();
Data d = new Data(1);
al.add(d);
d=new Data(2);al.add(d);d=new Data(3);al.add(d);

filterData(al, x -> x.value%2 == 0 );
System.out.println(al);
}
	public static void filterData(ArrayList<Data> dataList, Predicate<Data> p) {
		Iterator<Data> i = dataList.iterator();
		while (i.hasNext()) {
			if (p.test(i.next())) {
				i.remove();
			}
		}
}
}

but option A code snipet working fine why itis wrong.

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

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Post by admin »

Make sure you type the code exactly as given in the question.
If you like our products and services, please help us by posting your review here.

FanaticuS
Posts: 2
Joined: Sat Dec 07, 2019 10:19 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Post by FanaticuS »

Hi,

Shouldn't filterData(ArrayList<Data> dataList, Predicate<Data> p) method be static for the method call(from option B) to compile? Or am I missing something?

Regards,
A

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

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Post by admin »

Not necessarily. The code fragment could exist in an instance method of the same class in which the filterData method exists.
If you like our products and services, please help us by posting your review here.

Angelica
Posts: 2
Joined: Fri Sep 11, 2020 4:49 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Post by Angelica »

In the answer I see
"For a complete discussion on this topic please see this short tutorial - http://enthuware.com/index.php/home/115"

Unfortunately this link does not work. Is there a better one?

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

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Post by admin »

Yes, it should be https://enthuware.com/lambda-for-ocajp
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

f.motta
Posts: 6
Joined: Mon Mar 08, 2021 4:13 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Post by f.motta »

It didn't should have the "ArrayList<Data> al = new ArrayList<>()" declaration ? Or it is implicitly in the hidden "...." code?

And we must know Iterator for the exam?

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

Re: About Question enthuware.ocajp.i.v8.2.1469 :

Post by admin »

I see that ArrayList<Data> al = new ArrayList<Data>(); is there in the code.
Although Iterator is not explicitly mentioned in OCAJP 8 but it is good to know because iterating through a list using the for-each loop is on the exam and for-each requires an Iterator.
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 32 guests