1z0-809

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

Question 26

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 of fName and then ascending order of lName?

  1. .sorted (Comparator.comparing(Emp::getfName).reserved().thenComparing(Emp::getlName))
  2. .sorted (Comparator.comparing(Emp::getfName).thenComparing(Emp::getlName))
  3. .map(Emp::getfName).sorted(Comparator.reverseOrder())
  4. .map(Emp::getfName).sorted(Comparator.reverseOrder().map(Emp::getlName).reserved

Question 27

Given: public enum USCurrency { PENNY (1), NICKLE(5), DIME (10), QUARTER(25); private int value; public USCurrency(int value) { this.value = value; } public int getValue() {return value;} } public class Coin { public static void main (String[] args) { USCurrency usCoin =new USCurrency.DIME; System.out.println(usCoin.getValue()): } } Which two modifications enable the given code to compile? (Choose two.)

  1. Nest the USCurrency enumeration declaration within the Coin class.
  2. Make the USCurrency enumeration constructor public.
  3. Remove the new keyword from the instantion of usCoin.
  4. Make the getValue() method public.
  5. Add the final keyword in the declaration of value.

Question 28

Given: class ImageScanner implements AutoCloseable { public void close () throws Exception { System.out.print ("Scanner closed."); } public void scanImage () throws Exception { System.out.print ("Scan."); throw new Exception("Unable to scan."); } } class ImagePrinter implements AutoCloseable { public void close () throws Exception { System.out.print ("Printer closed."); } public void printImage () {System.out.print("Print."); } } and this code fragment: try (ImageScanner ir = new ImageScanner(); ImagePrinter iw = new ImagePrinter()) { ir.scanImage(); iw.printImage(); } catch (Exception e) { System.out.print(e.getMessage()); } What is the result?

  1. Scan.Printer closed. Scanner closed. Unable to scan.
  2. Scan.Scanner closed. Printer closed.Unable to scan.
  3. Scan. Unable to scan.
  4. Scan. Unable to scan. Scanner closed.

Question 29

Given the structure of the STUDENT table: Student (id INTEGER, name VARCHAR) Given: public class Test { static Connection newConnection =null; public static Connection get DBConnection () throws SQLException { try (Connection con = DriveManager.getConnection(URL, username, password)) { newConnection = con; } return newConnection; } public static void main (String [] args) throws SQLException { get DBConnection (); Statement st = newConnection.createStatement(); st.executeUpdate("INSERT INTO student VALUES (102, "˜Kelvin')"); } } Assume that: The required database driver is configured in the classpath. The appropriate database is accessible with the URL, userName, and passWord exists. The SQL query is valid. What is the result?

  1. The program executes successfully and the STUDENT table is updated with one record.
  2. The program executes successfully and the STUDENT table is NOT updated with any record.
  3. A SQLException is thrown as runtime.
  4. A NullPointerException is thrown as runtime.

Question 30

Given the code fragments: class Employee { Optional address; Employee (Optional address) { this.address = address; } public Optional getAddress() { return address; } } class Address { String city = "New York"; public String getCity { return city: } public String toString() { return city; } } and Address address = null; Optional addrs1 = Optional.ofNullable (address); Employee e1 = new Employee (addrs1); String eAddress = (addrs1.isPresent()) ? addrs1.get().getCity() : "City Not available"; What is the result?

  1. New York
  2. City Not available
  3. null
  4. A NoSuchElementException is thrown at run time.