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

Given:

class Student {

String course, name, city;

public Student (String name, String course, String city) {

this.course = course; this.name = name; this.city = city;

}

public String toString() {

return course + ":" + name + ":" + city;

}

and the code fragment:

List stds = Arrays.asList(

new Student ("Jessy", "Java ME", "Chicago"),

new Student ("Helen", "Java EE", "Houston"),

new Student ("Mark", "Java ME", "Chicago"));

stds.stream()

.collect(Collectors.groupingBy(Student::getCourse))

.forEach(src, res) -> System.out.println(scr));

What is the result?

    Correct Answer: D

    The code provided contains multiple issues leading to a compilation error. First, the method getCourse is not defined in the Student class. Additionally, the syntax in .forEach(src, res) -> System.out.println(scr)) is incorrect as it lacks proper parentheses and uses the wrong variable name. Correcting these issues would be necessary for the code to compile successfully.

Discussion
InnovationOption: D

a compile error occurs because the getCourse method is not defined if it was, the output would be Java EE Java ME

Ohayou

getCourse is added implicit by the compiler because variable course is not private. Correct Answer is B

shivkumarx

incorrect, Java compiler does not do this

ChoucouOption: D

Correct answer is D

rameasyOption: A

if It is students.stream().collect(Collectors.groupingBy(Studentee::getCourse)).forEach((src, res) -> System.out.println(src)); then the result is B. Java EE Java ME if It is students.stream().collect(Collectors.groupingBy(Studentee::getCourse)).forEach((src, res) -> System.out.println(res)); then the result is A. [Java EE: Helen:Houston] [Java ME: Jessy:Chicago, Java ME: Mark:Chicago]

OhayouOption: B

if we correct variable scr to src in line : forEach(src, res) -> System.out.println(scr)); Correct Answer is B else Correct Answer is D

lnrdgst

I agree. Also at ".forEach(src, res) -> System.out.println(scr));" it should be ".forEach((src, res) -> System.out.println(src));" Another however is that in the Student class there are no get methods. So answer is D

SampsOption: D

Correct Answer should be D Lambda is been (forEach Method) and the parameter is more than one and the Type wasn't specified

Ohayou

Collectors.groupingBy return Map<String, List<Student>> so we need forEach (BiConsumer) Method Correct Answer is B

asdfjhfgjuaDCVOption: D

Currently compilation error. stds.stream() .collect(Collectors.groupingBy(Student::getCourse)) .forEach(src, res) -> System.out.println(scr)); if code will be like above then B

steefaandOption: D

Compile fails since getCourse method is not defined.

r1muka5Option: D

Correct answer is D. Multiple compilation errors.

MahdiHamdiiOption: D

D there are too many errors I can count 3 1-missing parentheses near foreach 2- wrong variable in the sout 3- method getCourse isn't defined answer Would be D

WilsonKKerllOption: B

Answer is B...........

chichikimOption: B

.forEach(src, res) -> System.out.println(scr)); ===> .forEach((src, res) -> System.out.println(src)); Correct Answer is B

johnchen88Option: A

The Correct Answer should be A