Java Quiz: forEach Construct

Click to learn more about using the forEach construct.

Java 11 Certification Practice Questions on forEach

Q1. Given the following list

List<String> places = Arrays.asList("New York", "London", "Sydney");

Choose the options which give the same output as the following piece of code.

for(String place:places) {


A. places.forEach(System.out::println);
B. places.forEach(String place -> System.out.println(place));
C. places.forEach(place -> System.out.println(place));
D. places.forEach(System.out::println(place));


The given piece of code uses the enhanced for loop to iterate a list. Choices A and C are both correct ways to invoke forEach, which does the same thing. Choice A uses lambda expression, while choice C uses method reference. Choices B and D will not compile as the syntax used is incorrect.


For Java 11 Certification Exam Practice Questions, refer

Leave a Reply

Your email address will not be published. Required fields are marked *