1z0-819 Exam QuestionsBrowse all questions from this exam

1z0-819 Exam - Question 72


Given the code fragment:

Which can replace line 2?

Show Answer
Correct Answer: A

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.

Discussion

10 comments
Sign in to comment
xuancatOption: B
May 18, 2023

B is correct

StavokOption: B
Jun 7, 2023

B is Correct

MSEng
Jun 10, 2023

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

OmnisumemOption: B
Sep 8, 2023

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.

tmuralimanohar
Jun 29, 2023

Answer: none of the above. list is immutable, hence throws unsupportedOperation exception

StavokOption: A
Jul 17, 2023

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.

[Removed]
Aug 30, 2023

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

d7bb0b2Option: B
Jan 8, 2024

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

d7bb0b2
Jan 8, 2024

UnaryOperator<Integer> u = (Integer i) -> i * 2; OR THIS

mendjijetOption: B
Feb 8, 2024

B is ok but notice that list is immutable

ASPushkin
Jul 19, 2024

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