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

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;

}

public String getCourse() {return course;}

public String getName() {return name;}

public String getCity() {return 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: A

    The provided code contains a compilation error. Specifically, the usage of 'forEach(src, res) -> System.out.println(scr));' is incorrect. The correct syntax should be 'forEach((src, res) -> System.out.println(src));'. These issues prevent the code from compiling successfully.

Discussion
jduarteOption: A

Answer A. Tested

maslac

it is if you take a typo in question, that is likely, into consideration when testing, but if you correct this line .forEach(src, res) -> System.out.println(scr)); ==========> .forEach(src, res) -> System.out.println(src)); Then answer is B

steefaandOption: B

Answer is B assuming typo in forEach line.

duydnOption: B

.forEach(src, res) -> System.out.println(scr)); -> i think this is typo -> B if type not considered

duydn

*typo for line 2

jduarteOption: A

Answer is A, but if change stds.stream().collect(Collectors.groupingBy(Student::getCourse)) .forEach((src,res) -> System.out.println(src)); the answer is B

samtash1034

missing one 「(」?