Q1. Given the code fragment: List values = Arrays.asList (1, 2, 3); values.stream () .map(n -> n*2)//line n1 .peek(System.out::print)//line n2 .count(); What is the result? A. 246 B. The code produces no output. C. A compilation error occurs at line n1. D. A compilation error occurs at line n2. View AnswerAnswer: A Q2. Given the code fragment: public static void main (String [ ] args) throws IOException { BufferedReader br = new BufferedReader (new…
Q1. Which statement is true about the DriverManager class? A. It returns an instance of Connection. B. it executes SQL statements against the database. C. It only queries metadata of the database. D. it is written by different vendors for their specific database. View AnswerAnswer: A Explanation: The DriverManager returns an instance of Doctrine\DBAL\Connection which is a wrapper around the underlying driver connection (which is often…
Q1. Given the code fragment: 9. Connection conn = DriveManager.getConnection(dbURL, userName, passWord); 10. String query = “SELECT id FROM Employee”; 11. try (Statement stmt = conn.createStatement()) { 12. ResultSet rs = stmt.executeQuery(query); 13.stmt.executeQuery(“SELECT id FROM Customer”); 14. while (rs.next()) { 15. //process the results 16.System.out.println(“Employee ID: “+ rs.getInt(“id”)); 17.} 18. } catch (Exception e) { 19. System.out.println (“Error”); 20. } Assume that: The required database driver is configured in the classpath. The appropriate database is accessible with the dbURL, userName, and…
Q1. Given the code fragment: List colors = Arrays.asList(“red”, “green”, “yellow”); Predicate test = n - > { System.out.println(“Searching…”); return n.contains(“red”); }; colors.stream() .filter(c -> c.length() > 3) .allMatch(test); What is the result? A. Searching… B. Searching… Searching… C. Searching… Searching… Searching… D. A compilation error occurs. View AnswerAnswer: D Q2. Given the code fragment: List empDetails = Arrays.asList(“100, Robin, HR”, “200, Mary, AdminServices”, “101, Peter, HR”); empDetails.stream() .filter(s-> s.contains(“1”)) .sorted() .forEach(System.out::println); //line n1 What is the result? A. 100, Robin, HR 101, Peter,…
Q1. The data.doc, data.txt and data.xml files are accessible and contain text. Given the code fragment: Stream paths = Stream.of (Paths. get(“data.doc”), Paths. get(“data.txt”), Paths. get(“data.xml”)); paths.filter(s-> s.toString().endWith(“txt”)).forEach( s -> { try { Files.readAllLines(s) .stream() .forEach(System.out::println); //line n1 } catch (IOException e) { System.out.println(“Exception”); } } ); What is the result? A. The program prints the content of data.txt file. B. The program prints: Exception Exception C. A compilation error occurs at line n1. D. The program prints the content of the…
Q1. Which statement is true about the single abstract method of the java.util.function.Function interface? A. It accepts one argument and returns void. B. It accepts one argument and returns boolean. C. It accepts one argument and always produces a result of the same type as the argument. D. It accepts an argument and produces a result of any data type. View AnswerAnswer: C Reference: http://winterbe.com/posts/2014/03/16/java-8-tutorial/ (functions) Q2.…
Q1. Given: public class Emp { String fName; String lName; public Emp (String fn, String ln) { fName = fn; lName = ln; } public String getfName() { return fName; } public String getlName() { return lName; } } and the code fragment: List emp = Arrays.asList ( new Emp (“John”, “Smith”), new Emp (“Peter”, “Sam”), new Emp (“Thomas”, “Wale”)); emp.stream() //line n1 .collect(Collectors.toList()); Which code fragment, when inserted at line n1, sorts the employees list in descending order…
Q1. Given the for loop construct: for ( expr1 ; expr2 ; expr3 ) { statement; } Which two statements are true? A. This is not the only valid for loop construct; there exits another form of for loop constructor. B. The expression expr1 is optional. it initializes the loop and is evaluated once, as the loop begin. C. When expr2 evaluates to false, the loop terminates.…
Q1. 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? A. 10123 Ford 10124 BMW B. 10124 BMW 10123 Ford C. A compilation error occurs. D. A ClassCastException is thrown at run…
Q1. Which three statements are benefits of encapsulation? A. Allows a class implementation to change without changing t he clients B. Protects confidential data from leaking out of the objects C. Prevents code from causing exceptions D. Enables the class implementation to protect its invariants E. Permits classes to be combined into the same package F. Enables multiple instances of the same class to be created…