The only contextual code you may assume is the following:
With the side note that you may not assume that the name "
filter" is defined; it is just an example name.
I derived this context from the question:
a function of Predicate<String> type
The question is a bit poorly worded, but its intention is relatively clear.
Walter Rockett wrote:Accordingly, B and D would also be incorrect.
No. Answers B and D don't refer to any contextual code. The identifiers used in the body of those lambda expressions are declared in their respective parameter lists.
The larger problem however seems to be in the rationale provided in the review answer, I should have posted it. my apologies.
"Option E is incorrect because then name used in the parameter list does not match the name used in the body"
This seems clearly incorrect.
It's not. Option E accesses a variable
s, which is not defined. Therefore, option E is incorrect.
This is different from option B, for instance. Option B also accesses a variable
s, but it is declared at the start of the lambda expression, so it is valid code.
However, in keeping with the "trick you" spirit of the exam, I would be inclined to provide a code example with s1 in the enclosing scope and allow E to be one of the correct answers ;-)
While I do think that the exams appear to contain many tricky questions that really serve no educational purpose, this is not one of those questions. I would say this is one of the more straightforward questions I've come across.
Either way, provide some question code when the answer references actual code.
Yes, it would be better if the question had given some example code in which to insert the correct answer. However, in this case I think the code can be easily deduced from the problem statement.