1Z0-809 practice question 2 of 25
Given: class Student { String course, name, city; public Student (String name, String course, String city) { this.course = course; this.name = name…
Full set: 196 questionsThe code will fail to compile for two primary reasons. First, the method reference Student::getCourse requires a public method named getCourse() to be present in the Student class, which is missing. The Student class only defines public fields. Second, the lambda expression in the forEach call, (src, res) -> System.out.println(scr)), contains a typo. The variable scr is not defined; the parameter name is src. Either of these issues is sufficient to cause a compilation error.
A. The code will not compile, so it cannot produce this output. This output format also does not match what a Map.forEach printing only the key would produce.
B. The code will not compile. Even if it did, printing just the keys "Java EE" and "Java ME" would require a different lambda, such as (src, res) -> System.out.print(src).
C. The code will not compile. This output is similar to A but in a different, non-guaranteed order, and is also incorrect for the same reasons.
1. Oracle, The Java™ Tutorials, "Method References". (docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html). This tutorial explains that method references are compact lambda expressions for methods that already have a name. The syntax ClassName::methodName requires methodName to be a method.
2. The Java® Language Specification, Java SE 8 Edition, Section 15.13, "Method Reference Expressions". This section formally defines the syntax and semantics of method references, which apply to methods and constructors, not fields.
3. The Java® Language Specification, Java SE 8 Edition, Section 15.27.1, "Lambda Parameters". This section defines the scope of lambda parameters, confirming that a variable like scr would be an "unresolved symbol" if not declared as a parameter.
Keep practicing
Take the free 1Z0-809 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other 1Z0-809 practice questions
- Question 3Given: class Worker extends Thread { CyclicBarrier cb; public Worker(CyclicBarrier cb) { …
- Question 4Given the code fragment: Path file = Paths.get (“courses.txt”); // line n1 Assume the cou…
- Question 5Given that /green.txt and /colors/yellow.txt are accessible, and the code fragment: Path …
- Question 6Given the code fragment:List<String> empDetails = Arrays.asList(“100, Robin, HR”,“200, Ma…
- Question 7Given the code fragment: List str = Arrays.asList (“my”, “pen”, “is”, “your’, “pen”); Pre…
- Question 8Which two reasons should you use interfaces instead of abstract classes? (Choose two.)
