Given the code fragment:
List
System.out.println (
//line n1
);
Which code fragment must be inserted at line n1 to enable the code to print the maximum number in the nums list?
Given the code fragment:
List
System.out.println (
//line n1
);
Which code fragment must be inserted at line n1 to enable the code to print the maximum number in the nums list?
The correct code fragment to print the maximum number in the nums list is nums.stream().max(Comparator.comparing(a -> a)).get(). This uses a stream to compare each element and find the maximum value based on natural ordering.
A, tested. B will give the first number in the array list. C & D will give compilation error.
A is the correct answer. import java.util.Arrays; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(10, 20, 8); System.out.println("Maximum number: " + nums.stream() .max(Comparator.comparing(a -> a)) .get()); } }
Can confirm that correct answer is A. Tested.
Answer is A
A, tested
A,tested
I confirm