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?
All files and directories under the home directory are listed along with their attributes.
A compilation error occurs at line n1.
The files in the home directory are listed along with their attributes.
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?
10123 Ford 10124 BMW
10124 BMW 10123 Ford
A compilation error occurs.
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?
ur :: va
ueJa
The program prints nothing.
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?
A compilation error occurs. To rectify it, replace line n1 with: Test<Integer> type1 = new Test<>();
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?