Given the code fragment:
Which can replace line 2?
Given the code fragment:
Which can replace line 2?
The correct replacement for line 2 in the given code fragment is: 'UnaryOperator<Integer> u = (int i) -> i * 2;'. This line declares a UnaryOperator that takes an Integer, and the lambda expression multiplies the input by 2. The issues discussed about raw types can be resolved by using generics properly, making sure the lambda expression is correctly typed and syntactically correct.
Tested. All are wrong. Because all left side in answers are the same we should find correct expression on right side. Let say that on left side in all answers we have "UnaryOperator<Integer>" instead of "UnaryOperator". That make sense because left side is the same in all answers. In that case only B is valid.
There is no correct answer here. The specified UnaryOperators can only store objects. If they had types(<Integer> for instance), then A & D with some bit of syntax correction could have worked
B is Correct
B is correct
there is no right answer because of raw type in UnaryOperator declaration A. Failed error: incompatible types: incompatible parameter types in lambda expression UnaryOperator u = (int i)->i*2; ^ B. failed i is type of Object because of raw type bad operand types for binary operator '*' C. failed we can not skip the parenthesis in single parametr lambda D. failed i is type of Object because of raw type bad operand types for binary operator '*' and return with semicolons
B is ok but notice that list is immutable
None correct answer: but if the options have a Typed in declaration this two options are valid: B. UnaryOperator<Integer> u = (var i) -> i * 2; D. UnaryOperator<Integer> u = i -> i * 2; UnaryOperator<Integer> u = (int i) -> i * 2. => invalid, lamba only accepts wrapper clases not primitives C. UnaryOperator<Integer> u = var i -> { return i * 2; }; // bad sintax for var i cannot contain (var i) that is correct
UnaryOperator<Integer> u = (Integer i) -> i * 2; OR THIS
There's no answer here. As first of all: All of these UnaryOperators are of type UnaryOperator<Object> so there's not a single one valid. Secondly you can't modify a List created with List.of() so it will throw a unsupportedOperationException
The correct answer is `A. UnaryOperator u = (int i) -> i * 2;`. This line of code creates a `UnaryOperator` object that takes an `int` as input and returns the result of multiplying the input by 2. Option B is incorrect because the `var` keyword cannot be used in a lambda expression. Option C is incorrect because the syntax for a lambda expression with a single parameter does not allow the use of the `var` keyword. Option D is incorrect because the lambda expression is missing a closing brace.
Answer: none of the above. list is immutable, hence throws unsupportedOperation exception