Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 15

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 couList = Arrays.asList (

new Country ("Japan", Country.Continent.ASIA),

new Country ("Italy", Country.Continent.EUROPE),

new Country ("Germany", Country.Continent.EUROPE));

Map> regionNames = couList.stream ()

.collect(Collectors.groupingBy (Country ::getRegion,

Collectors.mapping(Country::getName, Collectors.toList()))));

System.out.println(regionNames);

    Correct Answer: B

    The code uses the Stream API to group countries by their continent and map their names to the corresponding continents. When the countries are processed, they are added to the map in the order provided thereby preserving the insertion order for each country's continent. The first country 'Japan' belongs to ASIA and the next two 'Italy' and 'Germany' belong to EUROPE. Thus, when printed, ASIA will have 'Japan' and EUROPE will have 'Italy' and 'Germany'. The grouping does not depend on enum declaration order but follows the order in which elements are streamed and collected, resulting in {ASIA = [Japan], EUROPE = [Italy, Germany]}. Hence, the correct answer is B.

Discussion
M_JawadOption: A

the correct answer is A , tested...

InnovationOption: A

A is correct

griglad

No look at the order of enum values. Asia goes first and then Europe

GaelBernardOption: A

Tested on JDoodle.com using JDK 8, 9, 10, 11, 17, always got the same result: A- {EUROPE=[Italy, Germany], ASIA=[Japan]}

WilsonKKerllOption: B

Answer is B. Because: new country("Japan", country.Continent.ASIA) this code is first

AstelitikOption: A

Answer is A. The order is important

Astelitik

Sorry, I meant B - {ASIA=[Japan], EUROPE=[Italy, Germany]}

rameasyOption: B

Ans: B.Tested.

ChoucouOption: B

correct answer is B

shivkumarxOption: A

I got A but I got B when using Java 17

asdfjhfgjuaDCVOption: B

B is the correct answer

steefaandOption: A

Answer is A.

Eden0516Option: B

Tested, answer is B

duydnOption: B

the order follow the senquential of ENUM constants

r1muka5Option: B

correct answer is B (with JDK8).

mevltOption: B

With JDK 8 the answer is B

bnagaraja9099Option: A

JDK 11 - returned A on testing.. {EUROPE=[Italy, Germany], ASIA=[Japan]}

Svetleto13Option: A

A,tested

Svetleto13

A with JDK14. B with JDK8.

Huim

I use JDK15, answer is B

Svetleto13

Maybe i typed wrong somewhere,but its actually B on both JDK8 and JDK14.If you change continent places in main method output is different.

griglad

Yeah but we dont care about what JDK14 says as this exam is for JDK8