Rec 06
Question (without answers):
Answers:
Problems
01. Rewrite method into one return statement using Maybe
The kind of question contains two steps:
Ignore any
nullcheck or equivalent (e.g.none()check)Write down the actual steps that do things
Choose the corresponding methods
So, follow the three steps, after step 1 and 2, we should find our that in this question, there are three things we should do
get the list of languages (by calling
r.getListofLanguages())check whether the list contains "Java"
If so, return the wrapper of
findInternship()as the method's return.
Now, moving on to step 3 — choose the corresponding API from Maybe.
r.getListofLanguages()will return a wrapper of "something". This tells us that we should useflatMap()instead ofmap()"check, contains" tells us that we should use
filter()findInternship()will return "something", but we need to wrap it first, then return it to a function. This tells us that we should usemap()instead offlatMap().
So, the final answer will be
02. Stack and Heap with Lambda
In CS2030s style,
The lambda expression should be explicitly written in the anonymous class name
The captured variable should be placed under a dashed line
So, in this question, why we have a as our captured variable? This is because according to the first rule from here:
The local variables of the method where the local class comes from will be captured.
So, in this unamed method, Line 2 is acutally creating an anonymous class (local class) inside this unamed method, and a is a local variable in this method, so, it will be captured!

Last updated