Java SE 8 Programmer II — Question 206

Given the definition of the Country class:
public class country {
public enum Continent {ASIA, EUROPE}
String name;
Continent region;
public Country (String na, Continent reg) {
name = na, region = reg;
}
public String getName () {return name;}
public Continent getRegion () {return region;}
}
and the code fragment:
List<Country> couList = Arrays.asList (
new Country ("Japan", Country.Continent.ASIA),
new Country ("Italy", Country.Continent.EUROPE),
new Country ("Germany", Country.Continent.EUROPE));
Map<Country.Continent, List<String>> regionNames = couList.stream ()
.collect(Collectors.groupingBy (Country ::getRegion,
Collectors.mapping(Country::getName, Collectors.toList()))));
System.out.println(regionNames);

Answer options

Correct answer: A

Explanation

The correct answer is A, as the code groups countries by their continent and lists their names. 'EUROPE' contains both 'Italy' and 'Germany', while 'ASIA' contains 'Japan'. The other options either misrepresent the grouping or incorrectly list names under continents.