tgoop.com/topJavaQuizExplain/272
Last Update:
What will be the output of the code?
❌ A. The sum of even numbers is 22
❌ B. The sum of even numbers is 24
✅ C. The sum of even numbers is 30
❌ D. The sum of even numbers is 54
❌ E. The code will not compile
❌ F. None of the above
Explanation:
The code creates a list of integers and uses the Stream API to filter the even numbers, convert them to primitive int values, and sum them up. The filter() method takes a lambda expression that tests whether a number is even by checking if it is divisible by 2. The mapToInt() method converts the Integer objects to int primitive values, which are required by the sum() method.
The resulting sum of the even numbers is 2 + 4 + 6 + 8 + 10 = 30.
Therefore, the correct answer is option C) The sum of even numbers is 30. The program will output "The sum of even numbers is 30" to the console.
BY Explanations “Top Java Quiz Questions”
Share with your friend now:
tgoop.com/topJavaQuizExplain/272