1z0-809

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

Question 21

Given the code fragments: class MyThread implements Runnable { private static AtomicInteger count = new AtomicInteger (0); public void run () { int x = count.incrementAndGet(); System.out.print (x+" "); } } and Thread thread1 = new Thread(new MyThread()); Thread thread2 = new Thread(new MyThread()); Thread thread3 = new Thread(new MyThread()); Thread [] ta = {thread1, thread2, thread3}; for (int x= 0; x < 3; x++) { ta[x].start(); } Which statement is true?

  1. The program prints 1 2 3 and the order is unpredictable.
  2. The program prints 1 2 3.
  3. The program prints 1 1 1.
  4. A compilation error occurs.

Question 22

Given the code fragment: public static void main (String [ ] args) throws IOException { BufferedReader br = new BufferedReader (new InputStremReader (System.in)); System.out.print ("Enter GDP: "); //line 1 } Which code fragment, when inserted at line 1, enables the code to read the GDP from the user?

  1. int GDP = Integer.parseInt (br.readline());
  2. int GDP = br.read();
  3. int GDP = br.nextInt();
  4. int GDP = Integer.parseInt (br.next());

Question 23

Assuming that the file /data/december/log.txt does not exist and given the code fragment: What is the result?

  1. A file with the name log.txt is created in the /data directory and the content of the /data/december/log.txt file is copied to it.
  2. The program executes successfully and does NOT change the file system.
  3. A FileNotFoundException is thrown at run time.
  4. A NoSuchFileException is thrown at run time.

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?

  1. [Java EE: Helen:Houston] [Java ME: Jessy:Chicago, Java ME: Mark:Chicago]
  2. Java EE Java ME
  3. [Java ME: Jessy:Chicago, Java ME: Mark:Chicago] [Java EE: Helen:Houston]
  4. A compilation error occurs.

Question 25

Given the code fragments: interface CourseFilter extends Predicate { public default boolean test (String str) { return str.equals ("Java"); } } and List strs = Arrays.asList("Java", "Java EE", "Java ME"); Predicate cf1 = s - > s.length() > 3; Predicate cf2 = new CourseFilter() { //line n1 public boolean test (String s) { return s.contains ("Java"); } }; long c = strs.stream() .filter(cf1) .filter(cf2 //line n2 .count(); System.out.println(c); What is the result?

  1. 2
  2. 3
  3. A compilation error occurs at line n1.
  4. A compilation error occurs at line n2.