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

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 » Tue Oct 13, 2015 10:50 am

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: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Wed Oct 14, 2015 9:36 pm

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 » Fri Apr 27, 2018 1:40 am

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: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri Apr 27, 2018 2:05 am

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.

limitime
Posts: 1
Joined: Tue Mar 12, 2019 12:40 am
Contact:

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

Post by limitime » Tue Mar 12, 2019 12:45 am

filterData(al, (Data x) -> x.value%2 == 0 );
The second option is marked incorrect, and the explanation is as below:
When all your method does is return the value of an expression, you can omit the curly braces, the return keyword, and the semi-colon from the method body part. Thus, instead of { return x.value%2 == 0; }, you can just write x.value%2 == 0


To me this answer is correct, maybe it's meant to have curly braces in it, but it doesn't. So I think either this option should be updated, or this question should have one more correct answer.

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

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

Post by admin » Tue Mar 12, 2019 2:46 am

I see that the second option, and only the second option, is marked as correct and the reason is as per the explanation that you have quoted. Please confirm.
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 1 guest