1z0-809

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

Question 31

Given the code fragment: Stream files = Files.walk(Paths.get(System.getProperty("user.home"))); files.forEach (fName -> { //line n1 try { Path aPath = fName.toAbsolutePath(); //line n2 System.out.println(fName + ":" + Files.readAttributes(aPath, Basic.File.Attributes.class).creationTime ()); } catch (IOException ex) { ex.printStackTrace(); }); What is the result?

  1. All files and directories under the home directory are listed along with their attributes.
  2. A compilation error occurs at line n1.
  3. The files in the home directory are listed along with their attributes.
  4. A compilation error occurs at line n2.

Question 32

Given: class Vehicle { int vno; String name; public Vehicle (int vno, String name) { this.vno = vno,; this.name = name; } public String toString () { return vno + ":" + name; } } and this code fragment: Set vehicles = new TreeSet (); vehicles.add(new Vehicle (10123, "Ford")); vehicles.add(new Vehicle (10124, "BMW")); System.out.println(vehicles); What is the result?

  1. 10123 Ford 10124 BMW
  2. 10124 BMW 10123 Ford
  3. A compilation error occurs.
  4. A ClassCastException is thrown at run time.

Question 33

Given that course.txt is accessible and contains: Course : : Java - and given the code fragment: public static void main (String[ ] args) { int i; char c; try (FileInputStream fis = new FileInputStream ("course.txt"); InputStreamReader isr = new InputStreamReader(fis);) { while (isr.ready()) { //line n1 isr.skip(2); i = isr.read (); c = (char) i; System.out.print(c); } } catch (Exception e) { e.printStackTrace(); } } What is the result?

  1. ur :: va
  2. ueJa
  3. The program prints nothing.
  4. A compilation error occurs at line n1.

Question 34

Given: public class Test { private T t; public T get () { return t; } public void set (T t) { this.t = t; } public static void main (String args [ ] ) { Test 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?

  1. Java 100
  2. java.lang.string@<hashcode>java.lang.Integer@<hashcode>
  3. A compilation error occurs. To rectify it, replace line n1 with: Test<Integer> type1 = new Test<>();
  4. A compilation error occurs. To rectify it, replace line n2 with: type1.set (Integer(100));

Question 35

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?

  1. Make the Vehicle class public.
  2. Make the name variable public.
  3. Make the getName method public.
  4. Make the name variable private.
  5. Make the setName method private.
  6. Make the getName method private.