1z0-809

1z0-809 practice questions, page 8 of 10, with answer review and explanations.

Question 36

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 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?

  1. 2 : 30
  2. 4 : 0
  3. 4 : 60
  4. 4 : 60 2 : 30 3 : 20 1 : 10
  5. The program prints nothing.

Question 37

Given the code fragments: public class Book implements Comparator { String name; double price; public Book () {} public Book(String name, double price) { this.name = name; this.price = price; } public int compare(Book b1, Book b2) { return b1.name.compareTo(b2.name); } public String toString() { return name + ":" + price; } } and List books = Arrays.asList ( new Book ("Beginning with Java", 2), new book ("A Guide to Java Tour", 3) ); Collections.sort(books, new Book()); System.out.print(books); What is the result?

  1. [A Guide to Java Tour:3.0, Beginning with Java:2.0]
  2. [Beginning with Java:2.0, A Guide to Java Tour:3.0]
  3. A compilation error occurs because the Book class does not override the abstract method compareTo().
  4. An Exception is thrown at run time.

Question 38

Given the code fragment: List listVal = Arrays.asList("Joe", "Paul", "Alice", "Tom"); System.out.println ( // line n1 ); Which code fragment, when inserted at line n1, enables the code to print the count of string elements whose length is greater than three?

  1. listVal.stream().filter(x -> x.length()>3).count()
  2. listVal.stream().map(x -> x.length()>3).count()
  3. listVal.stream().peek(x -> x.length()>3).count().get()
  4. listVal.stream().filter(x -> x.length()>3).mapToInt(x -> x).count()

Question 39

Given the code fragments: class Caller implements Callable { String str; public Caller (String s) {this.str=s;} public String call()throws Exception { return str.concat ("Caller");} } class Runner implements Runnable { String str; public Runner (String s) {this.str=s;} public void run () { System.out.println (str.concat ("Runner"));} } and public static void main (String[] args) InterruptedException, ExecutionException { ExecutorService es = Executors.newFixedThreadPool(2); Future f1 = es.submit (new Caller ("Call")); Future f2 = es.submit (new Runner ("Run")); String str1 = (String) f1.get(); String str2 = (String) f2.get(); //line n1 System.out.println(str1+ ":" + str2); } What is the result?

  1. The program prints: Run Runner Call Caller : null And the program does not terminate.
  2. The program terminates after printing: Run Runner Call Caller : Run
  3. A compilation error occurs at line n1.
  4. An Execution is thrown at run time.

Question 40

Given: public class Canvas implements Drawable { public void draw () { } } public abstract class Board extends Canvas { } public class Paper extends Canvas { protected void draw (int color) { } } public class Frame extends Canvas implements Drawable { public void resize () { } } public interface Drawable { public abstract void draw (); } Which statement is true?

  1. Board does not compile.
  2. Paper does not compile.
  3. Frame does not compile.
  4. Drawable does not compile.
  5. All classes compile successfully.