1Z0-809 Exam - Java SE 8 Programmer II

certleader.com

Q1. The data.doc, data.txt and data.xml files are accessible and contain text. 

Given the code fragment: 

Stream<Path> paths = Stream.of (Paths. get(“data.doc”), 

Paths. get(“data.txt”), 

Paths. get(“data.xml”)); 

paths.filter(s-> s.toString().endWith(“txt”)).forEach( 

s -> { 

try { 

Files.readAllLines(s) 

.stream() 

.forEach(System.out::println); //line n1 

} catch (IOException e) { 

System.out.println(“Exception”); 

); 

What is the result? 

A. The program prints the content of data.txt file. 

B. The program prints: 

Exception 

<<The content of the data.txt file>> 

Exception 

C. A compilation error occurs at line n1. 

D. The program prints the content of the three files. 

Answer:

Q2. 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); 

What is the output? 

A. {EUROPE = [Italy, Germany], ASIA = [Japan]} 

B. {ASIA = [Japan], EUROPE = [Italy, Germany]} 

C. {EUROPE = [Germany, Italy], ASIA = [Japan]} 

D. {EUROPE = [Germany], EUROPE = [Italy], ASIA = [Japan]} 

Answer:

Q3. Which statement is true about java.util.stream.Stream? 

A. A stream cannot be consumed more than once. 

B. The execution mode of streams can be changed during processing. 

C. Streams are intended to modify the source data. 

D. A parallel stream is always faster than an equivalent sequential stream. 

Answer:

Q4. Given the code fragment: What is the result? 

A. 20 

B. 25 

C. 29 

D. Compilation fails 

E. AnArrayIndexOutOfBoundsException is thrown at runtime 

Answer:

Q5. Given the definition of the Vehicle class: 

class Vehicle { 

String name; 

void setName (String name) { 

this.name = name; 

String getName() { 

return name; 

Which action encapsulates the Vehicle class? 

A. Make the Vehicle class public. 

B. Make the name variable public. 

C. Make the setName method public. 

D. Make the name variable private. 

E. Make the setName method private. 

F. Make the getName method private. 

Answer:

Q6. Given: 

public class product { int id; int price; 

public Product (int id, int price) { 

this.id = id; 

this.price = price; 

public String toString() { return id + “:” + price; } 

and the code fragment: 

List<Product> products = Arrays.asList(new Product(1, 10), 

new Product (2, 30), 

new Product (2, 30)); 

Product p = products.stream().reduce(new Product (4, 0), (p1, p2) -> { 

p1.price+=p2.price; 

return new Product (p1.id, p1.price);}); 

products.add(p); 

products.stream().parallel() 

.reduce((p1, p2) - > p1.price > p2.price ? p1 : p2) 

.ifPresent(System.out: :println); 

What is the result? 

A. 2 : 30 

B. 4 : 0 

C. 4 : 60 

D. 4 : 60 

2 : 30 

3 : 20 

1 : 10 

E. 

The program prints nothing. 

Answer:

Q7. Given the code fragment: 

int b = 3; 

if ( !(b > 3)) { 

System.out.println("square "); 

}{ 

System.out.println("circle "); 

System.out.println("..."); 

What is the result? 

A. square... 

B. circle... 

C. squarecircle... 

D. Compilation fails. 

Answer:

Q8. Which two reasons should you use interfaces instead of abstract classes? 

A. You expect that classes that implement your interfaces have many common methods or fields, or require access modifiers other than public. 

B. You expect that unrelated classes would implement your interfaces. 

C. You want to share code among several closely related classes. 

D. You want to declare non-static on non-final fields. 

E. You want to take advantage of multiple inheritance of type. 

Answer: A,E 

Reference: http://www.programmerinterview.com/index.php/java-questions/interface-vs-abstract-class/ 

Q9. 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<Student> 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? 

A. [Java EE: Helen:Houston] 

[Java ME: Jessy:Chicago, Java ME: Mark:Chicago] 

B. Java EE 

Java ME 

C. [Java ME: Jessy:Chicago, Java ME: Mark:Chicago] 

[Java EE: Helen:Houston] 

D. A compilation error occurs. 

Answer:

Q10. Given: 

public class Test<T> { 

private T t; 

public T get () { 

return t; 

public void set (T t) { 

this.t = t; 

public static void main (String args [ ] ) { 

Test<String> type = new Test<>(); 

Test type 1 = new Test ();//line n1 

type.set(“Java”); 

type1.set(100);//line n2 

System.out.print(type.get() + “ “ + type1.get()); 

What is the result? 

A. Java 100 

B. java.lang.string@<hashcode>java.lang.Integer@<hashcode> 

C. A compilation error occurs. To rectify it, replace line n1 with: Test<Integer> type1 = new Test<>(); 

D. A compilation error occurs. To rectify it, replace line n2 with: type1.set (Integer(100)); 

Answer: