1z0-809 practice questions, page 3 of 10, with answer review and explanations.
Question 11
Given the code fragment: Stream> iStr= Stream.of ( Arrays.asList ("1", "John"), Arrays.asList ("2", null)); Stream nInSt = iStr.flatMapToInt ((x) -> x.stream ()); nInSt.forEach (System.out :: print); What is the result?
1John2null
12
A NullPointerException is thrown at run time.
A compilation error occurs.
Question 12
Given the code fragment: Path file = Paths.get ("courses.txt"); // line n1 Assume the courses.txt is accessible. Which code fragment can be inserted at line n1 to enable the code to print the content of the courses.txt file?
List<String> fc = Files.list(file); fc.stream().forEach (s - > System.out.println(s));
Stream<String> fc = Files.readAllLines (file); fc.forEach (s - > System.out.println(s));
List<String> fc = readAllLines(file); fc.stream().forEach (s - > System.out.println(s));
Stream<String> fc = Files.lines (file); fc.forEach (s - > System.out.println(s));
Question 13
Given the code fragment: public void recDelete (String dirName) throws IOException { File [ ] listOfFiles = new File (dirName) .listFiles(); if (listOfFiles ! = null && listOfFiles.length >0) { for (File aFile : listOfFiles) { if (aFile.isDirectory ()) { recDelete (aFile.getAbsolutePath ()); } else { if (aFile.getName ().endsWith (".class")) aFile.delete (); } } } } Assume that Projects contains subdirectories that contain .class files and is passed as an argument to the recDelete () method when it is invoked. What is the result?
The method deletes all the .class files in the Projects directory and its subdirectories.
The method deletes the .class files of the Projects directory only.
The method executes and does not make any changes to the Projects directory.
The method throws an IOException.
Question 14
Given the code fragments: 4. void doStuff() throws ArithmeticException, NumberFormatException, Exception { 5. if (Math.random() >-1 throw new Exception ("Try again"); 6. } and 24. try { 25. doStuff ( ): 26. } catch (ArithmeticException | NumberFormatException | Exception e) { 27. System.out.println (e.getMessage()); } 28. catch (Exception e) { 29. System.out.println (e.getMessage()); } 30. } Which modification enables the code to print Try again?
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 couList = Arrays.asList ( new Country ("Japan", Country.Continent.ASIA), new Country ("Italy", Country.Continent.EUROPE), new Country ("Germany", Country.Continent.EUROPE)); Map > regionNames = couList.stream () .collect(Collectors.groupingBy (Country ::getRegion, Collectors.mapping(Country::getName, Collectors.toList())))); System.out.println(regionNames);
{EUROPE = [Italy, Germany], ASIA = [Japan]}
{ASIA = [Japan], EUROPE = [Italy, Germany]}
{EUROPE = [Germany, Italy], ASIA = [Japan]}
{EUROPE = [Germany], EUROPE = [Italy], ASIA = [Japan]}