Quiz

It is possible to employ multiple computer cores to filter a list. Which statement will filter with increased performance? books.parallelStream().filter(book - > { return book.getTitle().startsWith("E"); }).forEach(System.out::println); Correct books.pStream().filter(book - > { return book.getTitle().startsWith("E"); }).forEach(System.out::println); books.streamParallel().filter(book - > { return book.getTitle().startsWith("E"); }).forEach(System.out::println); books.streamp().filter(book - > { return book.getTitle().startsWith("E"); }).forEach(System.out::println); ****************************************************************************************************** Which statement is equivalent to the following code? GreetingMessage gm = new GreetingMessage() { @Override public void greet(String name) { System.out.println("Hello " + name); } GreetingMessage gm = (String name) -> { greet("Hello " + name); }; Incorrect GreetingMessage gm = (String name) -> { System.out.println("Hello " + name); }; Correct GreetingMessage gm = (String name) => { System.out.println("Hello " + name); }; GreetingMessage gm => (String name) { System.out.println("Hello " + name); }; ******************************************************************************************************* A method reference is a special lambda expression that calls an existing method on an object. Which statement uses the correct method reference syntax? Shapes shapes = Square->calculateArea(); Incorrect Shapes shapes = Square.calculateArea(); Incorrect Shapes shapes = Square=>calculateArea; Shapes shapes = Square::calculateArea; Correct ***************************************************************************************************** Q.4 What is the benefit of using streams in Java? 1. Any method can have input and output stream functionality. 2. Data can be printed after each iteration. 3. Successive filters can be chained together in a computationally efficient manner. 4.Streams can be used in conjunction with a forEach loop. Ans:- 3. Successive filters can be chained together in a computationally efficient manner. ******************************************************************************************************* How would you implement a functional interface called WarningMessage with a method called warn? @FunctionalInterface public class WarningMessage { public void warn(String message); } @FunctionalInterface public interface WarningMessage { public abstract void warn(String message); } Correct @FunctionalInterface public class WarningMessage { public abstract void warn(String message); } @InterfaceClass public class WarningMessage { public abstract void warn(String message); } Ans :- @FunctionalInterface public interface WarningMessage { public abstract void warn(String message); } Correct ************************************************************************************************** Q. If you want to create a list of files inside the current directory, what is the correct code to accomplish this? 1. FilenameFilter filter => (file, fileName) -> { return fileName.contains("."); }; String[] contents = new File().list(filter); 2. FilenameFilter filter = (file, fileName) -> { return fileName.contains("/"); }; String[] contents = new File(".").list(filter); 3. FilenameFilter filter = (file, fileName) -> { return fileName.contains("."); }; String[] contents = new File(".").list(filter); Correct 4. FilenameFilter filter = (file, fileName) -> { return fileName.contains("/"); }; String[] contents = new File("..").list(filter); Ans:- 3. FilenameFilter filter = (file, fileName) -> { return fileName.contains("."); }; String[] contents = new File(".").list(filter); Correct ********************************************************************************************************************************* Q. What is the safest way to handle creating a new file? 1. File myFile = new File("mytext.txt"); try { boolean fileCreated = myFile.createNewFile(); System.out.println(fileCreated); } catch (IOException ioe) { } Correct 2. File myFile = new File("mytext.txt"); boolean fileCreated = myFile.createNewFile(); System.out.println(fileCreated); catch (IOException ioe) { } 3. File myFile = new File("mytext.txt"); catch (IOException ioe) { boolean fileCreated = myFile.createNewFile(); System.out.println(fileCreated); } 4. File myFile = new File("mytext.txt"); try { } catch (IOException ioe) { boolean fileCreated = myFile.createNewFile(); System.out.println(fileCreated); } Ans :- 1. File myFile = new File("mytext.txt"); try { boolean fileCreated = myFile.createNewFile(); System.out.println(fileCreated); } catch (IOException ioe) { } Correct *************************************************************************************************************** How can you copy a source file to a destination file, regardless of whether the destination file is present? Files.copy(source, dest, OVERWRITE_EXISTING); Files.copy(source, dest, REPLACE); Incorrect Files.copy(source, dest, REPLACE_EXISTING); Correct Files.copy(source, dest, APPEND_EXISTING); Incorrect ******************************************************************************************************************

Comments

Popular posts from this blog

What are POJO classes in spring? What is the use of POJO Class, and How to create POJO classes.

How to create React project using command prompt?