Given: class Worker extends Thread { CyclicBarrier cb; public Worker(CyclicBarrier cb) { this.cb = cb; } public void run () { try { cb.await(); System.out.println("Worker..."); } catch (Exception ex) { } } } class Master implements Runnable { //line n1 public void run () { System.out.println("Master..."); } } and the code fragment: Master master = new Master(); //line n2 Worker worker = new Worker(cb); worker.start(); You have been asked to ensure that the run methods of both the Worker and Master classes are executed. Which modification meets the requirement?
-
A
At line n2, insert CyclicBarrier cb = new CyclicBarrier(2, master);
-
B
Replace line n1 with class Master extends Thread {
-
C
At line n2, insert CyclicBarrier cb = new CyclicBarrier(1, master);
-
D
At line n2, insert CyclicBarrier cb = new CyclicBarrier(master);
Reveal answer details
Close answer details
Given the code fragment: Stream<Path> files = Files.list(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?
-
A
All files and directories under the home directory are listed along with their attributes.
-
B
A compilation error occurs at line n1.
-
C
The files in the home directory are listed along with their attributes.
-
D
A compilation error occurs at line n2.
Reveal answer details
Close answer details
Given the code fragment:  Which should be inserted into line n1 to print Average = 2.5?
-
A
IntStream str = Stream.of (1, 2, 3, 4);
-
B
IntStream str = IntStream.of (1, 2, 3, 4);
-
C
DoubleStream str = Stream.of (1.0, 2.0, 3.0, 4.0);
-
D
Stream str = Stream.of (1, 2, 3, 4);
Reveal answer details
Close answer details
Given:  and  Which interface from the java.util.function package should you use to refactor the class Txt?
-
A
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answerC
ExplanationReferences: https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
Given: public class Counter { public static void main (String[ ] args) { int a = 10; int b = -1; assert (b >=1) : "Invalid Denominator"; int = a / b; System.out.println (c); } } What is the result of running the code with the -ea option?
-
A
-
B
-
C
An AssertionError is thrown.
-
D
A compilation error occurs.
Reveal answer details
Close answer details
Given the code fragment:  Which is the valid definition of the Course enum?
-
A
-
B
-
C
-
D
Reveal answer details
Close answer details
Given the code fragment: List<String> 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, HR
-
B
A compilation error occurs at line n1.
-
C
100, Robin, HR 101, Peter, HR 200, Mary, AdminServices
-
D
100, Robin, HR 200, Mary, AdminServices 101, Peter, HR
Reveal answer details
Close answer details
Given the code fragment: public static void main (String[] args) throws IOException { BufferedReader brCopy = null; try (BufferedReader br = new BufferedReader (new FileReader("employee.txt"))) { // line n1 br.lines().forEach(c -> System.out.println(c)); brCopy = br; //line n2 } brCopy.ready(); //line n3; } Assume that the ready method of the BufferedReader, when called on a closed BufferedReader, throws an exception, and employee.txt is accessible and contains valid text. What is the result?
-
A
A compilation error occurs at line n3.
-
B
A compilation error occurs at line n1.
-
C
A compilation error occurs at line n2.
-
D
The code prints the content of the employee.txt file and throws an exception at line n3.
Reveal answer details
Close answer details
Given the code fragment: List<Integer> nums = Arrays.asList (10, 20, 8): System.out.println ( //line n1 ); Which code fragment must be inserted at line n1 to enable the code to print the maximum number in the nums list?
-
A
nums.stream().max(Comparator.comparing(a -> a)).get()
-
B
nums.stream().max(Integer : : max).get()
-
C
-
D
nums.stream().map(a -> a).max()
Reveal answer details
Close answer details
Question 10
Single choice
Given: class FuelNotAvailException extends Exception { } class Vehicle { void ride() throws FuelNotAvailException { //line n1 System.out.println("Happy Journey!"); } } class SolarVehicle extends Vehicle { public void ride () throws FuelNotAvailException { //line n2 super ride (); } } and the code fragment: public static void main (String[] args) throws Exception { Vehicle v = new SolarVehicle ();
-
A
ride(); } Which modification enables the code fragment to print Happy Journey!?
-
B
Replace line n1 with public void ride() throws FuelNotAvailException {
-
C
Replace line n1 with protected void ride() throws Exception {
-
D
Replace line n2 with void ride() throws Exception {
-
E
Replace line n2 with private void ride() throws FuelNotAvailException {
Reveal answer details
Close answer details
Question 11
Single choice
Given that data.txt and alldata.txt are accessible, and the code fragment:  What is required at line n1 to enable the code to overwrite alldata.txt with data.txt?
-
A
-
B
-
C
-
D
Reveal answer details
Close answer details
Question 12
Single choice
Given the code fragment:  Which code fragment, when inserted at line n1, enables the code to print /First.txt?
-
A
Path iP = new Paths ("/First.txt");
-
B
Path iP = Paths.toPath ("/First.txt");
-
C
Path iP = new Path ("/First.txt");
-
D
Path iP = Paths.get ("/", "First.txt");
Reveal answer details
Close answer details
Question 13
Single choice
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?
-
A
-
B
-
C
The program prints nothing.
-
D
A compilation error occurs at line n1.
Reveal answer details
Close answer details
Question 14
Single choice
Given the code fragments: interface CourseFilter extends Predicate<String> { public default boolean test (String str) { return str.equals ("Java"); } } and List<String> strs = Arrays.asList("Java", "Java EE", "Java ME"); Predicate<String> cf1 = s - > s.length() > 3; Predicate cf2 = new CourseFilter() { //line n1 public boolean test (String s) { return s.contains ("Java"); } }; long c = strs.stream() .filter(cf1) .filter(cf2 //line n2 .count(); System.out.println(c); What is the result?
-
A
-
B
-
C
A compilation error occurs at line n1.
-
D
A compilation error occurs at line n2.
Reveal answer details
Close answer details
Question 15
Single choice
Given the code fragment: UnaryOperator<Double> uo1 = s -> s*2; //line n1 List<Double> loanValues = Arrays.asList(1000.0, 2000.0); loanValues.stream() .filter(lv -> lv >= 1500) .map(lv -> uo1.apply(lv)) //line n2 .forEach(s -> System.out.print(s + " ")); What is the result?
-
A
-
B
-
C
A compilation error occurs at line n1.
-
D
A compilation error occurs at line n2.
Reveal answer details
Close answer details
Question 16
Single choice
Given that these files exist and are accessible: /sports/info.txt /sports/cricket/players.txt /sports/cricket/data/ODI.txt and given the code fragment: int maxDepth =2; Stream<Path> paths = Files.find(Paths.get("/sports"), maxDepth, (p, a) -> p.getFileName().toString().endsWith ("txt"), FileVisitOption.FOLLOW_LINKS); Long fCount = paths.count(); System.out.println(fCount); Assuming that there are NO soft-link/symbolic links to any of the files in the directory structure, what is the result?
-
A
-
B
-
C
-
D
An Exception is thrown at runtime.
Reveal answer details
Close answer details
Question 17
Single choice
Given the code fragments:  and  What is the result?
-
A
Video played.Game played.
-
B
A compilation error occurs.
-
C
class java.lang.Exception
-
D
class java.io.IOException
Reveal answer details
Close answer details
Question 18
Single choice
Given the code fragment:  Assume that: The required database driver is configured in the classpath. The appropriate database is accessible with the dbURL, userName, and passWord exists. The Employee and Customer tables are available and each table has id column with a few records and the SQL queries are valid. What is the result of compiling and executing this code fragment?
-
A
The program prints employee IDs.
-
B
The program prints customer IDs.
-
C
The program prints Exception.
-
D
compilation fails on line 13.
Reveal answer details
Close answer details
Question 19
Single choice
Given records from the Player table:  and given the code fragment: try { Connection conn = DriverManager.getConnection(URL, username, password); Statement st= conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); st.execute ("SELECT * FROM Player"); st.setMaxRows(2); ResultSet rs = st.getResultSet(); rs.absolute(3); while (rs.next ()) { System.out.println(rs.getInt(1) + " " + rs.getString(2)); } } catch (SQLException ex) { System.out.print("SQLException is thrown."); } Assume that: The required database driver is configured in the classpath. The appropriate database is accessible with URL, username, and password. The SQL query is valid. What is the result?
-
A
-
B
The program prints nothing.
-
C
-
D
Reveal answer details
Close answer details
Question 20
Single choice
Given the code fragment: Stream<List<String>> iStr= Stream.of ( Arrays.asList ("1", "John"), Arrays.asList ("2", null)0; Stream<<String> nInSt = iStr.flatMapToInt ((x) -> x.stream ()); nInSt.forEach (System.out :: print); What is the result?
-
A
-
B
-
C
A NullPointerException is thrown at run time.
-
D
A compilation error occurs.
Reveal answer details
Close answer details
Question 21
Multiple choice
For which three objects must a vendor provide implementations in its JDBC driver? (Choose three.)
-
A
-
B
-
C
-
D
-
E
-
F
-
G
Reveal answer details
Close answer details
Correct answersC, D, E
ExplanationDatabase vendors support JDBC through the JDBC driver interface or through the ODBC connection. Each driver must provide implementations of java.sql.Connection, java.sql.Statement, java.sql.PreparedStatement, java.sql.CallableStatement, and java.sql.Re sultSet. They must also implement the java.sql.Driver interface for use by the generic java.sql.DriverManager interface.
Question 22
Single choice
Given the code fragment:  Which statement can be inserted into line n1 to print 1,2; 1,10; 2,20;? {System.out.print (i + "," + j+ "; ");};
-
A
BiConsumer<Integer,Integer> c = (i, j) ->
-
B
BiFunction<Integer, Integer, String> c = (i, j) ?gt; {System.out.print (i + "," + j+ "; ")};
-
C
BiConsumer<Integer, Integer, String> c = (i, j) ?gt; {System.out.print (i + "," + j+ "; ")};
-
D
BiConsumer<Integer, Integer, Integer> c = (i, j) ?gt; {System.out.print (i + "," + j+ "; ");};
Reveal answer details
Close answer details
Correct answerB
ExplanationReferences: https://www.concretepage.com/java/jdk-8/java-8-biconsumer-bifunction-bipredicate-example
Question 23
Single choice
Given: public final class IceCream { public void prepare() {} } public class Cake { public final void bake(int min, int temp) {} public void mix() {} } public class Shop { private Cake c = new Cake (); private final double discount = 0.25; public void makeReady () { c.bake(10, 120); } } public class Bread extends Cake { public void bake(int minutes, int temperature) {} public void addToppings() {} } Which statement is true?
-
A
A compilation error occurs in IceCream.
-
B
A compilation error occurs in Cake.
-
C
A compilation error occurs in Shop.
-
D
A compilation error occurs in Bread
-
E
All classes compile successfully.
Reveal answer details
Close answer details
Question 24
Single choice
Given:  and the code fragment:  What is the result?
-
A
A compilation error occurs at line n2.
-
B
A compilation error occurs because the try block doesn't have a catch or finally block.
-
C
A compilation error occurs at line n1.
-
D
The program compiles successfully.
Reveal answer details
Close answer details
Question 25
Single choice
Given:  and the code fragment:  Which modification enables the code fragment to print Speaker?
-
A
Implement Predicate in the Product.ProductFilter class and replace line n2 with .filter (p -> p.ProductFilter.test (p))
-
B
Replace line n1 with: public static boolean isAvailable (Product p) {
-
C
Replace line n2 with: .filter (p -> p.ProductFilter: :isAvailable (p))
-
D
Replace line n2 with: .filter (p -> Product: :ProductFilter: :isAvailable ())
Reveal answer details
Close answer details
Question 26
Single choice
Given the definition of the Employee class:  and this code fragment:  What is the result?
-
A
[sales:Ada, hr:Bob, sales:Bob, hr:Eva]
-
B
[Ada:sales, Bob:sales, Bob:hr, Eva:hr]
-
C
[hr:Eva, hr:Bob, sales:Bob, sales:Ada]
-
D
[hr:Bob, hr:Eva, sales:Ada, sales:Bob]
Reveal answer details
Close answer details
Question 27
Single choice
Given the code fragment: BiFunction<Integer, Double, Integer> val = (t1, t2) -> t1 + t2; //line n1 System.out.println(val.apply(10, 10.5)); What is the result?
-
A
-
B
-
C
A compilation error occurs at line n1.
-
D
A compilation error occurs at line n2.
Reveal answer details
Close answer details
Question 28
Single choice
Which code fragment is required to load a JDBC 3.0 driver?
-
A
Connection con = Connection.getDriver ("jdbc:xyzdata://localhost:3306/EmployeeDB");
-
B
Class.forName("org.xyzdata.jdbc.NetworkDriver");
-
C
Connection con = DriverManager.getConnection ("jdbc:xyzdata://localhost:3306/EmployeeDB");
-
D
DriverManager.loadDriver ("org.xyzdata.jdbc.NetworkDriver");
Reveal answer details
Close answer details
Question 29
Single choice
Given the code fragment: public class Foo { public static void main (String [ ] args) { Map<Integer, String> unsortMap = new HashMap< > ( ); unsortMap.put (10, "z"); unsortMap.put (5, "b"); unsortMap.put (1, "d"); unsortMap.put (7, "e"); unsortMap.put (50, "j"); Map<Integer, String> treeMap = new TreeMap <Integer, String> (new Comparator<Integer> ( ) { @Override public int compare (Integer o1, Integer o2) {return o2.compareTo (o2); } } ); treeMap.putAll (unsortMap); for (Map.Entry<Integer, String> entry : treeMap.entrySet () ) { System.out.print (entry.getValue () + " "); } } } What is the result?
-
A
A compilation error occurs.
-
B
-
C
-
D
Reveal answer details
Close answer details
Question 30
Single choice
Given the code fragment: Map<Integer, String> books = new TreeMap<>(); books.put (1007, "A"); books.put (1002, "C"); books.put (1001, "B"); books.put (1003, "B"); System.out.println (books); What is the result?
-
A
{1007 = A, 1002 = C, 1001 = B, 1003 = B}
-
B
{1001 = B, 1002 = C, 1003 = B, 1007 = A}
-
C
{1002 = C, 1003 = B, 1007 = A}
-
D
{1007 = A, 1001 = B, 1003 = B, 1002 = C}
Reveal answer details
Close answer details
Correct answerB
ExplanationReferences: TreeMap inherits SortedMap and automatically sorts the element's key
Question 31
Single choice
Given:  and the code fragment:  What is the result?
-
A
-
B
An AgeOutOfLimitException is thrown.
-
C
A UserException is thrown.
-
D
A compilation error occurs in the doRegister method.
Reveal answer details
Close answer details
Question 32
Single choice
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?
-
A
The program executes successfully and the STUDENT table is updated with one record.
-
B
The program executes successfully and the STUDENT table is NOT updated with any record.
-
C
A SQLException is thrown as runtime.
-
D
A NullPointerException is thrown as runtime.
Reveal answer details
Close answer details
|