Blog Java Free Questions on Oracle Certified Professional, Java SE 8 Programmer (OCPJP 8)...
Java SE 8 Programmer certification

Free Questions on Oracle Certified Professional, Java SE 8 Programmer (OCPJP 8) (1Z0 809)

The Oracle Certified Professional, Java SE 8 Programmer Certification exam (1Z0-809) requires you to have a practical understanding and Hands-on experience with Java SE8 version. The prerequisite for this certification is the completion of the Oracle Certified Associate(OCA) certification.

In this article, we provide Java SE 8 Programmer certification 1Z0-809 free test questions. These 1Z0-809 exam questions are for your easy assessment of the certification exam, and to give you an idea on the pattern of the questions that could be asked in the actual exam.

Domain : Java Class Design

Q1 : Given

  1.        interface Switchable{

  2.                     void sw(int i);

  3.        }

  4.        abstract class Switch{

  5.                     abstract void sw(int i);

  6.        }

  7.        class It _ _ _ _ _ _ _ _ _ _ _ _ _{

  8.        

  9.                    public void sw(int i){}

  10.     

  11.                 public static void main(String []arg){

  12.                                 System.out.print(“A”);

  13.                 }

  14.    }

 Which is true? 

A. At line 7 extending the abstract class “Switch” is more  appropriate than implementing the interface “Switchable”
B. At line 7 implementing the interface “Switchable” is more  appropriate than extending the abstract class  “Switch” 
C. At line 7 implementing the interface or extending the interface won’t give us any advantage over another
D. Since the both the interface and the abstract class are abstract, both of them will provide same flexibility
E. Implementing or extending will fail this code from compiling

Correct Answer: B

Explanation

B is correct as in this code both the interface and the abstract class provide same functionality  to class “It” but implementing the interface is preferable as it allows “It” class to extend another class when needed.
A is incorrect as if we extends the abstract class then we can’t extend another class if needed.
C and D are incorrect as in this case, interface clearly provides us more flexibility over abstract class.
E is incorrect as we have correctly overload the “sw()” method at line 9.

Reference: http://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html

 

Domain : Building Database Applications with JDBC

Q2 : Which of the following isolation level will block all kind of reads on a row until the transaction is committed?

A. TRANSACTION_READ_COMMITTED
B. TRANSACTION_READ_UNCOMMITTED
C. TRANSACTION_ REPEATABLE_READ
D. TRANSACTION_SERIALIZABLE
E. TRANSACTION_READ

Correct Answer: D

Explanation

Refer to the following table about the Isolation Level provided in the oracle reference site.

Isolation Level Transactions Dirty Reads Non-Repeatable Reads Phantom Reads
TRANSACTION_NONE Not supported Not applicable Not applicable Not applicable
TRANSACTION_READ_COMMITTED Supported Prevented Allowed Allowed
TRANSACTION_READ_UNCOMMITTED Supported Allowed Allowed Allowed
TRANSACTION_REPEATABLE_READ Supported Prevented Prevented Allowed
TRANSACTION_SERIALIZABLE Supported Prevented Prevented Prevented

According to above table it is clear that the isolation level “TRANSACTION_SERIALIZABLE” will block all kind of reads on a row until the transaction is committed. So the correct option is D.

Reference: http://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html

 

Domain : Exceptions and Assertions

Q3 : Consider following three statements (select true statements ) .

  1. A try with resources statement without a catch block requires a finally block.

  2. A try with resources statement without a finally block requires a catch block.

  3. A try with resources statement with only one statement can omit the {}

A. Only I
B. Only III
C. Only I and II
D. Only II and III
E. None of above

Correct Answer: E

Explanation

Option E is correct since the all given three statements are false. A try-with-resources statement does not require a catch or finally block where a traditional try statement requires at least one of the two. Even with one statement it is manadory to have {} with try with resources.

Reference: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

 

Domain : Generics and Collections

Q4 : Given

  1.        import java.util.*;

  2.        import java.util.function.Predicate;

  3.        

  4.        public class Whizlab{

  5.         

  6.         public static void main(String[] args){

  7.         

  8.                List<Integer> numbers = new ArrayList<>();

  9.                 

  10.         numbers.add(10);

  11.        numbers.add(11);

  12.         numbers.add(13);

  13.         numbers.add(19);

  14.         numbers.add(5);

  15.                                

  16.                 Predicate<Integer> predit = p -> p > 10; 

  17.                                

  18.                 numbers.stream().filter(predit);

  19.                                

  20.                 System.out.println(numbers);

  21.     }

  22.    }

What is the output?

[10, 5]
[11,  13, 19]
[10, 11, 13, 19, 5]
Compilation fails due to error at line 16
Compilation fails due to error at line 18

Correct Answer: C

Explanation

java.util.function.Predicate interface defines an abstract method named test(T t) that accepts an object of generic type T and returns a boolean primitive.  At line 16, we have passed valid lambda expression which takes an Integer object in and return true if it is greater than 10.

At line 18 ,  we called stream method on numbers so it will become a stream, then we invoke filter method by passing the created predicate. But there is no any terminal operation after filter method. Streams are lazy because intermediate operations are not evaluated unless terminal operation is invoked.  Streams never make changes to the source object. So output contains all values of the list, hence option C is correct.

Reference: http://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html#laziness

 

Domain : Java Class Design

Q5 : Given

  1.        public class Whiz{

  2.         

  3.                     static int x;

  4.                    

  5.                     public static void main(String[] args){

  6.                                     Whiz w1 = new Whiz ();

  7.                                     Whiz w2 = new Whiz ();

  8.                                     Whiz w3 = new Whiz ();

  9.                                     w1.method();

  10.                                w2.method();

  11.                                 w3.method();

  12.                 }

  13.                

  14.                 public void method(){

  15.                                 while(++x<3){

  16.                                             System.out.print(“A”);

  17.                                 }

  18.                 }

  19.    }                         

What is the output?

A. AAAAAA
B. AA
C. No output
D. Compilation fails due to error on line 15
E. Compilation fails due to multiple errors

Correct Answer: B

Explanation

Static variable x” at line 3 has default value “0” as we have not given any specific value. So when the “method()” invokes first time using “w1” object reference, while loop executes and will print “A” twice and final value of the variable “x” is 3. Since the variable “x” is static every other object share same value of the variable “x”. So when w1 and w2 objects try invoke method(), while loop wouldn’t invoke because x is already reached to 3.

Reference: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

 

Domain : Java Concurrency

Q6 : Which of the followings are true? 

A. There is no different between Callable’s “call()” method and Runnable’s “run()”method
B. Callable’s “call()” method allows to return some value while Runnable’s “run()” doesn’t
C. Callable is a class while Runnable is an interface
D. Both the Callable and Runnable interfaces have only one method
E. Both Callable’s “call()” method and Runnable’s “run()” methods can’t be overridden to throw checked exception when necessary

Correct Answers: B and D

Explanation

Option B is correct as the Callable’s “call()” method allows it to return some value.
Option D is correct as both the Callable and Runnable interfaces have only one method. The Callable interface has only the “call()” method and the Runnable interface only got the “run()” method.
Option A is incorrect as there are different between Callable’s “call()” method and Runnable’s “run()”method. Callable’s “call()” method can return a value and it also can throw exceptions while Runnable’s “run()”method doesn’t.
Option C is incorrect as the Callable is an interface like the Runnable.
Option E is incorrect as the Callable’s “call()” method can be overridden to throw checked exception when necessary.

Reference: http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Callable.html

 

Domain : Java Class Design

Q7 : Given

  1.        public class Whiz{

  2.               public static void main(String[] args){

  3.                               I2.print();       

  4.                               I1.print();       

  5.               }

  6.        }

  7.         

  8.        interface I1{

  9.               public static void print(){

  10.                           System.out.print(“1”);

  11.           }

  12.    }

  13.     

  14.    interface I2 extends I1{      

  15.           public static void print(){

  16.                           System.out.print(“2”);

  17.           }

  18.    }

What is the output?

A. 11
B. 22
C. 12
D. 21
E. Compilation fails

Correct Answer: D

Explanation

Static methods in interfaces are not inherited so there is no problem in defining new static method with same name in I2. Hence output will be 2 and 1. So option D is correct.

Reference: https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

 

Domain : Building Database Applications with JDBC

Q8 : You are asked to establish a connection to a MySql database in the “localhost”, called “School”. You are given the password as “abcd” and the user name as the “admin”.
Which of the following connection URL / arguments can be passed to getConnection method of DriverManager ?

A. “jdbc:mysql://localhost:3306/School?”+”user=admin&password=abcd” 
B. “jdbc:mysql://School:3306?user=admin&password=abcd”
C. “jdbc:mysql://localhost:3306/School”,”abcd”,”admin”  
D. “jdbc:mysql://School:3306/localhost?user=admin&password=abcd”
E. “jdbc:mysql://:3306/School”,”admin”, “abcd”  

Correct Answers: A and E

Explanation

getConnection method of DriverManager class is overloaded. 

public static Connection getConnection(String url) throws SQLException

public static Connection getConnection(String url, Properties info) throws SQLException

public static Connection getConnection(String url,String user, String password) throws SQLException

jdbc:mysql://[host][:port]/[database][?property1][=value1]…

Host – The host name where MySQL server is running. Default is 127.0.0.1 – the IP address of localhost.

Port – The port number where MySQL is listening for connection. Default is 3306.

Database – The name of an existing database on MySQL server. If not specified, the connection starts no current database.

Property – The name of a supported connection properties. “user” and “password” are 2 most important properties.

Value – The value for the specified connection property.

According to given scenario, host should be localhost, database should be school, user name should be “admin” and the password should be “abcd”.

Option A is correct as it follows correct format and pass the parameters in correct order.
Option E is correct as when we don’t specify  the host name default host will be 127.0.0.1 which is for localhost.
Option B and D are incorrect as we have passed the “School” as the host name.
Option C is incorrect as password and user name are in incorrect order.

References: http://docs.oracle.com/javase/tutorial/jdbc/overview/index.html, http://www.oracle.com/technetwork/java/overview-141217.html

 

Domain : Exceptions and Assertions

Q9 : Given ( assertions are enabled )

  1.        public class Whizlab{

  2.                     public static void main(String[] args) {

  3.                                     int j = 9;

  4.                                    assert(++j > 7): “Error”;

  5.                                     assert(j==12): j;

  6.                                     assert(++j > 8): System.out.println(j);

  7.                                   

  8.                     }

  9.        }

What is the output?

A. 8
B. 9
C. Compilation fails due to line 5
D. Compilation fails due to line 6
E. Compilation fails due to multiple errors

Correct Answer: D

Explanation

Option D Is correct as the code fails to compile only due to line 6. When an assert statement has two expressions, the second expression must return a value. The only two-expression assert statement that doesn’t return a value is on line 6.

Reference: http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html

 

Domain : Generics and Collections

Q10 : Given

  1. import java.util.ArrayList;

  2. import java.util.List;

  3.  

  4. public class Whiz{

  5.             

  6.              public static void main(String[] args){

  7.                              List list = new ArrayList();

  8.                              list.add(“1”);

  9.                              list.add(“2”);

  10.                              list.add(“3”);

  11.                              list.add(“4”);

  12.                             

  13.                              System.out.println(list.set(3,”3”));

  14.              }

  15. }  

What is the output?

A. 4
B. 3
C. -1
D. An Exception is thrown
E. Compilation fails

Correct Answer: A

Explanation

We have used ArrayList class set method at line 13,

public E set(int index, E element)

This method replaces the element at the specified position in this list with the specified element, and returns the element which is removed. So in this case 4 will be returned since 4 is located in the position we are going to remove. So option A is correct.

Reference: http://docs.oracle.com/javase/tutorial/collections/interfaces/List.html

 

Domain : Java Class Design

Q11 : Given

  1.        public class Pro {

  2.         

  3.                     static int i;

  4.                    

  5.                     public static void main(String[] args){

  6.                                     Pro p1 = new Pro();

  7.                                     Pro p2 = new Pro();

  8.                                     Pro p3 = new Pro();

  9.                                     p1.method();

  10.                                 p2.method();

  11.                                 p3.method();

  12.                 }

  13.                

  14.                public void method(){

  15.                                 while(++i<3){

  16.                                 System.out.print(“A”);

  17.                                 }

  18.                 }

  19.    }                         

What is the output?

A. AAAAAA
B. AA
C. No output
D. Compilation fails due to error on line 15
E. Compilation fails due to multiple errors

Correct Answer: B

Explanation

Static variable “i” at line 3 has default value “0” as we have not given any specific value. So when the “method()” invokes first time using “p1” object reference, while loop executes and will print “A” twice. Since the variable “i” is static “p2” and “p3” objects sees variable “i” with value larger than 3, so when using “p2” and “p3” for invoking the “method()”, while loop will not execute.

The output only contains two “A”s as explained above, so option B is correct.
Option A is incorrect, it would be correct if the variable “i” was a instance variable.
Options D and E are incorrect as the code compiles fine. 

Reference: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

 

Domain : Java Concurrency

Q12 : Given

  1.        import java.util.concurrent.atomic.*;

  2.        public class Whizlab{

  3.         

  4.         private static class CStore implements Runnable{

  5.                     int x=1;

  6.         private AtomicInteger cps = new AtomicInteger(10);

  7.        

  8.                     public void run(){

  9.                                     while(x > 0){

  10.                                                 try{

  11.                                                                 Thread.sleep(500);

  12.                                                 }catch(Exception e){

  13.                                                                System.out.print(e);

  14.                                                 }

  15.                                                 synchronized(this){

  16.                                                                 x = cps.decrementAndGet();

  17.                                                 }

  18.                                System.out.print(cps);

  19.                                 }

  20.                 }

  21.     }

  22.     

  23.     public static void main(String args[]){

  24.                 CStore cs = new CStore();

  25.    

  26.                                 Thread th1 = new Thread(cs);

  27.                                 Thread th2 = new Thread(cs);               

  28.                                 th1.start();

  29.                                 th2.start();

  30.        }

  31.    }

Which of the following could be an output?

A. Always 9876543210
B. Might be 9876543210-1
C. An Exception
D. Never ending loop
E. Compilation fails

Correct Answer: B

Explanation

Since Java 1.5 the java language provides atomic variables, e.g. AtomicInteger or AtomicLong which provide methods like getAndDecrement(), getAndIncrement() and getAndSet() which are atomic. Before the inception of the Java SE 5 atomic classes, programmers were forced to write synchronized code blocks around multithreaded objects. These synchronized blocks are inefficient and an added burden on the developer. The java.util.concurrent.atomic package relieved this burden by providing atomic equivalents to primitive types as well as object references.

Here, we have created a static class called “CStore” and it has implemented the Runnable interface. In this class we have created an AtomicInteger of value 10. Using a while loop and the AtomicInteger class “decrementAndGet()” method, we have tried to print the numbers from 10 to 0 in a reverse order in the overridden “run()” method.

AtomicInteger class’ “decrementAndGet()”method will cause the reduction 1 from the current value of the AtomicInteger variable and return the reduced value. So in this code, in every iterate of the while loop, that returning value of the AtomicInteger class’ “decrementAndGet()”method will be assigned to the variable “x”.

Both th1 and th2 are working on same “cs” object. In the every iteration of while loop, one thread invokes the run it’ll go to sleep mode for 500ms after checking the current value of the variable “x”. When the value of the “x” equal to 1 both threads see that x>0, so both threads will reduce 1 from the current value of the “x”, so the output will contain -1. So the option B is correct.

Here using applying the synchronization at line 15 is not needed as the “cps” is an AtomicInteger and the method “decrementAndGet()” perform an atomic operation. We could avoid the -1, if we synchronized the checking of the value of the “x” at line 9. So we can see that, even with Atomic variables. Memory consistence errors are still possible.

Other options are incorrect based on above logic.

References: http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html, http://docs.oracle.com/javase/tutorial/essential/concurrency/atomicvars.html

 

Domain : Java File I/O (NIO.2)

Q13 : Given :

  1.     import java.io.IOException;

  2.     import java.nio.file.*;

  3.     import java.nio.file.attribute.*;

  4.     import java.util.regex.Pattern;

  5.     public class Mat{

  6.     static boolean f;

  7.                 public static void main(String []args)throws IOException{

  8.                             Path path = Paths.get(“I:\\whizlabs”);

  9.                                 FileVisitor<Path> searcher = new Search();

  10.                                 Files.walkFileTree(path, searcher);

  11.                     if(!f)System.out.print(“No matches”);

  12.             }

  13.             private static final class Search extends SimpleFileVisitor<Path> {

  14.                                 public FileVisitResult postVisitDirectory(Path dir, IOException e)throws IOException {

  15.                                                 Pattern p = Pattern.compile(“…”);

  16.                                                 PathMatcher pm = FileSystems.getDefault().getPathMatcher(“glob:…”);

  17.                                                 if(pm.matches(dir.getFileName())){

  18.                                                                 System.out.println(dir);

  19.                                                                 f=true;

  20.                                                 }

  21.                                                 return FileVisitResult.CONTINUE;

  22.                                 }

  23.                 }

  24.    }

Note: assume that the I:\whizlabs actually exists and contain files and directories and you have enough permission.
Which is the result?

A. Completion succeeds and “No matches” will be printed as the output
B. Completion succeeds and prints only the paths of the “whizlabs” directory and its subdirectories
C. Completion succeeds and prints only the paths of the subdirectories of the “whizlabs” directory which contains only three letters for directory name
D. An exception will be thrown at the runtime
E. Compilation fails

Correct Answer: A

Explanation

We can use “java.nio.file.PathMatcher” interface to perform match operations on paths. To have a “PathMatcher”, we can use the “getPathMatcher()” method in the “java.nio.file.FileSystem” class. We should pass a String to the method and that String work as the pattern for matching operations. We can use “regex” syntax or “glob” syntax for matching, when we pass the pattern, using the “regex:” prefix, we indicates that we are passing the “regex” syntax. If we pass “glob” syntax, then we use the “glob:” prefix to the pattern.

We can use the “wakFileTree()” method combining with the “PathMatcher” to recursively perform path matching operations.

Option A is correct as the “wakFileTree()” method visit all the files and subdirectories of the given file root, the “postVisitDirectory ()” method is invoked after the all entries are visited. Then the method compares the current directory with the “PathMatcher”. As we have passed the glob syntax “”, there will be no matching path because not like with regex syntax, with glob syntax, “.” has no special meaning so this glob syntax only matches with a names which are created with three “.”.
Option B and C are incorrect as explained above.
Option D is incorrect as we have given that the directory actually exists and we have enough permission.
Option E is incorrect as code compiles fine.

References: http://docs.oracle.com/javase/tutorial/essential/io/find.html, http://docs.oracle.com/javase/8/docs/api/java/nio/file/FileSystem.html#getPathMatcher(java.lang.String)

 

Domain : Building Database Applications with JDBC

Q14 : Given

  1.        import java.sql.*;

  2.        public class Pro{

  3.         

  4.         public static void main(String []args)throws SQLException{

  5.                     DBConnect dc = new DBConnect();

  6.                     dc.insert(6,”Martha”,”Kent”,”2012-10-10″);

  7.         }

  8.        }

  9.         

  10.        class DBConnect{

  11.               private Connection con;

  12.            private Statement stm;

  13.               private ResultSet rs;

  14.    

  15.              public DBConnect(){

  16.                      // Assume: A connection object and a Statement object are successfully created in     this constructor.

  17.              }

  18.    

  19.              public void insert(int id, String fname, String lname, String date)throws SQLException{

  20.     try{

  21.                 String query = “INSERT INTO users VALUES( null , ‘” + fname + “‘, ‘”+lname+ “‘, ‘”+date+”‘)”;

  22.             stm.executeQuery (query);

  23.                    }finally{

  24.                             con.close();

  25.                    }

  26.             }

  27.     }

Note: You can consider given table as the “users” table. And consider the ID can be auto increment.Users Table JavaWhich of the following is true?

A. A new record will be added as follows:Users Table JavaB. A new record will be added as follows:C. Code will run without any exception but no record will be added
D. Compilation fails due to error at line 22
E. An exception will be thrown at runtime

Correct Answer: E

Explanation

Option E is correct as a SQLException is thrown at runtime. INSERT is a data manipulation statement (DML). In this code, trying to execute DML statement using the “executeQuery()” method will cause the SQLException. To execute such a query, we should have used the “execute()” method or the “executeUpdate()” method. So line 22, will cause an exception.

If we used “execute()” method or the “executeUpdate()”  method, the output could be B. Because ID can be auto increment, passing “null” to ID won’t cause any problem. If we pass null, it will automatically choose a suitable value.

References: http://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html, http://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html

 

Domain : Exceptions and Assertions

Q15 : Given

  1.         class Whizlab{

  2.          

  3.                  public static void main(String args[]){

  4.                                  int x = 10, y = 0;

  5.                                  try{

  6.                                                  int c = x/y;

  7.                                  }

  8.                                  catch(IllegalArgumentException | NullPointerException e){

  9.                                                  System.out.print(“Multi”);

  10.                                }

  11.                                catch(Exception e){

  12.                                                System.out.print(“Exc”);

  13.                                }

  14.                }

  15.       }                                                                                                                                

Which is the output?

A. No output
B. Multi
C. Exc
D. An uncaught exception will be thrown
E. Compilation fails

Correct Answer: C

Explanation

Option C is correct as at line 6, an ArithmeticException is thrown but it couldn’t be caught by the multiple exception catch box so the exception is passed to the catch box at line 11, so “Exc” will be printed.
Option A is incorrect as an ArithmeticException is thrown at the runtime.
Option B is incorrect as the ArithmeticException couldn’t be caught by the multiple exception catch box.
Option D is incorrect as there is no uncaught exception
Option E is incorrect as code compiles fine.

Reference: http://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html

 

Domain : Generics and Collections

Q16 : Consider
             class Whizlab<Double> { }
Which of the following is valid initialization of the class Whizlab?

A. Whizlab<> my = new Whizlab<>();
B. Whizlab<int> my = new Whizlab<>();
C. Whizlab<String>  my = new Whizlab<>();
D. Whizlab<>  my = new Whizlab(); 
E. None of above

Correct Answer: C

Explanation

A generic class is defined with the following format:

class name { /* … */ }

The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, …, and Tn.

So in given statement we have use Double as the type name, but note that it is not the type of the generic. Hence here when we initialize we can use any valid type like given in option C.

Options B and D incorrect since we can’t use primitive type as generic

Reference: https://docs.oracle.com/javase/tutorial/java/generics/types.html

 

Domain : Java Concurrency

Q17 : Given the following method :

  1.   public Long compute() {

  2.                          

  3.         if (high – low <= MAX) {

  4.  

  5.           long sum = 0;

  6.  

  7.           for (int i = low; i < high; ++i)

  8.                        sum += array[i];

  9.                   return sum;

  10.           } else {

  11.                 int mid = low + (high – low) / 2;

  12.                 Sum left  = new Sum(array, low, mid);

  13.                 Sum right = new Sum(array, mid, high);

  14.                 long rightAns = right.compute();

  15.                 left.fork();

  16.                 long leftAns  = left.join();

  17.                 return leftAns + rightAns;

  18.           }

  19.   }                  

Which of the following statements is true?

A. Code fragment shows the appropriate way of using the Fork-Join
B. Code fragment doesn’t show the appropriate way of using the Fork-Join
C. Code fragment will fail the compilation due to error on line 17
D. None of above

Correct Answer: B

Explanation

Option B is correct as we have called “compute()” method recursively before calling the “fork()” method so if we used this order then we would have no parallelism since this computes the “right” before starting to compute left. So this is not the appropriate way of using the Fork-Join.
Option C is incorrect as calling these methods in any order won’t cause any compile time error.

Reference: http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html

 

Domain : Java File I/O (NIO.2)

Q18 : Given

  1. import java.nio.file.*;

  2. import java.io.IOException;

  3. import java.util.*;

  4.     class Whizlab{

  5.     public static void main(String [] args)throws IOException{

  6.                 Path path = Paths.get(“..\\myfile.txt”);

  7.                

  8.          }

  9.    }

Which, inserted independently at line 7, will compile and produce the owner of the “myfile.txt” as the output?
I. Map<String, Object> mp = new HashMap();
   mp = Files.readAttributes(path, “*” );
   System.out.print(mp.get(“owner”));
II. BasicFileAttributes at = Files.readAttributes(path, BasicFileAttributes.class);
    System.out.print(at.owner ());
III. System.out.print(Files.getOwner (path));

A. Only I
B. Only I and II
C. Only III
D. None
E. All

Correct Answer: C

Explanation

Option C is correct as only the code fragment III will compiles and produces the expected output.

The code fragment I, is incorrect as there is one overloaded version of java.nio.file.Files class’ “readAttributes()” method which takes path object and a string (and a LinkOption  but it’s optional).  By using it we can have a Map of basic-file-attributes. We can specify which basic-file-attributes we need to retrieve, by using the String argument. Here we have passed “owner”, hoping that,it will return a map object only containing the owners name of the file which path object referring but the “owner” attribute is not an one of basic-file-attributes so the output will be “null” as there is no key called “owner” in the returning map.

Other version of the java.nio.file.Files class’ “readAttributes()” will cause compilation fails as there is no method called “owner()” in the “BasicFileAttributes”.

References: http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html, http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html, http://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/BasicFileAttributes.html

 

Domain : Java Stream API

Q19 : Given

  1.        import java.util.Optional;

  2.         

  3.        public class Whizlab{

  4.                     public static void main(String[] args){

  5.                                     Optional<Integer> ops = Optional.of(new Integer(10));

  6.                                     ops.filter(x -> Integer.compare(12, x) > 0).ifPresent(System.out::print);

  7.                                     System.out.println(ops.get());             

  8.                     }

  9.        }

What is the output?

A. 10
B. 1010
C. Optional[10]
D. A NoSuchElementException
E. Compilation fails

Correct Answer: B

Explanation

At line 5 we have created an instance of optional with an Integer wrapper. Then at line 6 invoking the filter method by passing “x -> Integer.compare(12, x) > 0” as the predicate will not filter that optional element hence when the invoking ifPresent there is valid element inside the optional. So it will execute the consumer of the ifPresent method which is printing statement. So 10 will be printed then at line 7 get method will return the element of Optional hence again 10 will be printed. Hence option B is correct.

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html

 

Domain : Generics and Collections

Q20 : If we add Enum constants to a tree set what will be the order in which they will be maintained?

A. Insertion order
B. Order in which constants are declared
C. Reverse  sorting  order
D. Won’t maintain any order

Correct Answer: B

Explanation

Enum implements Comparable via the natural order of the enum (the order in which the values are declared), so if we add enum to a sorted collection like tree set it will maintain the order in which constants are declared. Hence option B is correct.

Reference: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

 

Domain : Java Concurrency

Q21 : Given

  1.     import java.util.concurrent.locks.*;

  2.     import java.util.concurrent.*;

  3.     import java.util.*;

  4.     class Whizlab{

  5.     

  6.     static class Task1 implements Callable<Integer>{

  7.                 public Integer call(){

  8.                                 return  1;

  9.                 }

  10.     }

  11.    static class Task2 implements Callable<Integer>{

  12.                public Integer call(){

  13.                                 return 2;

  14.                 }

  15.     }

  16.     static class Task3 implements Callable<Double>{

  17.                 public Double call(){

  18.                                 return 3.0;

  19.                 }

  20.     }

  21.     public static void main(String []args)throws Exception{

  22.                 final ExecutorService pool = Executors.newFixedThreadPool(2);

  23.                 List<Callable<Integer>> cal = new ArrayList<>();

  24.                

  25.                 cal.add(new Task2());

  26.                 cal.add(new Task1());

  27.                                

  28.                 System.out.print(pool.invokeAny(cal));

  29.                 pool.shutdownNow();

  30.     }

  31.    }

Which is true about the above code? 

A. Compilation fails
B. An exception will be thrown at runtime
C. Only 1 or 2 will be printed as the output
D. Only 2 will be printed as the output
E. 2 1 Will be printed as the output

Correct Answer: C

Explanation

In this code, we have created three Callable objects (Task1, Task2, Task3). Then we have created a FixedThreadPool with two threads. This type of pool always has a specified number of threads running; if a thread is somehow terminated while it is still in use, it is automatically replaced with a new thread. Tasks are submitted to the pool via an internal queue. Then we have added Task2 and Task1 to a ArrayList. The invokeAny() method takes a collection of Callable objects, or subinterfaces of Callable. Invoking this method does not return a Future, but returns the result of one of the Callable objects. You have no guarantee about which of the Callable’s results you get. So in this code invoking the “invokeAny()” method at line 28, will return 1 or 2 but not both. So the answer is C.

Options D and E are incorrect as explained above.
Option A is incorrect as the code compiles fine.

References: http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html, http://docs.oracle.com/javase/tutorial/essential/concurrency/exinter.html

 

Domain : Generics and Collections

Q22 : Given

  1.  import java.util.ArrayList;

  2.  import java.util.List;

  3.     

  4.  public class Whiz{

  5.         public static void main(String[] args){

  6.               List<Integer> ints = new ArrayList<>();

  7.          

  8.               ints.add(1);

  9.               ints.add(2);   

  10.                   ints.add(3);

  11.                   ints.replaceAll(i -> i/2);                           

  12.               System.out.println(ints.stream().distinct().count());

  13.          }

  14.    }

What is the output?

A. 1
B. 2
C. 3
D. An exception is thrown
E. Compilation fails

Correct Answer: B

Explanation

The replaceAll method of the List interface replaces each element of this list with the result of applying the operator to that element.

 default void replaceAll(UnaryOperator operator)

 The default implementation is equivalent to, for this list:

final ListIterator li = list.listIterator();

while (li.hasNext()) {

li.set(operator.apply(li.next()));

}

So here we have passed UnaryOperator to divide elements by 2 and then assigned them to each position, hence it will result in a list to contain, 0, 1 and 1. At line 12 invoking distinct will remove duplicate 1 so count will be 2. Hence option B is correct.

Reference: http://docs.oracle.com/javase/tutorial/collections/interfaces/List.html

 

Domain : Generics and Collections

Q23 : Given

  1.     import java.util.Map;

  2.     import java.util.TreeMap;

  3.     

  4.     public class Whizlab{

  5.                

  6.                 public static void main(String args[]){

  7.                                 Map<String, Integer> tmap =  new TreeMap<>();

  8.                                 tmap.put(“ab”,1);

  9.                                 tmap.put(“abc”,2);

  10.                                 tmap.put(“abcd”,3);

  11.                                 tmap.replaceAll( (k,v) -> (int)k.charAt(v) );

  12.                                 System.out.println(tmap.values());

  13.                 }

  14.    } 

What is the output?

A. [1, 2, 3]
B. [98, 98, 98]
C. [98, 99, 100]
D. An Exception
E. Compilation fails

Correct Answer: C

Explanation

Java 8 introduced a new method called replaceAll in the Map interface. It replaces each entry’s value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. Exceptions thrown by the function are related to the caller.

public void replaceAll(BiFunction function)

So here at line 11 we have passed BiFunction to get the char at index of value from the key string and then to return the ascii value of the char. So in this case values will be replaced with their keys char value of the given position. SO option C is correct.

Reference: http://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html

 

Domain : Java File I/O (NIO.2)

Q24 : Given :

  1.     import java.nio.file.*;

  2.     import java.io.*;

  3.     import java.nio.file.attribute.*;

  4.     

  5.     class NIO{

  6.             public static void main(String [] args)throws IOException{

  7.                         Path path = Paths.get(“..\\myfile.txt”);

  8.                         //

  9.             }

  10.       }

Note: assume that the myfile.txt actually exists and code runs on a windows flat form and the” myfile.txt” file is already marked as read only and you have enough permission to do any operation on the file.
Which, inserted independently at line 9, will compile and produce the group owner of the file.of the “myfile.txt” as the output?

I. PosixFileAttributes pa =  Files.readAttributes(path, PosixFileAttributes.class);            System.out.print(pa.group().getName());

II. PosixFileAttributes pa =  Files.readAttributes(path, PosixFileAttributes.class); System.out.print(pa.group());

A. Only I will cause a compile time error
B. Only II will cause a compile time error
C. Both will cause compile time error
D. None of the above is true

Correct Answer: D

Explanation

Option D is correct as “PosixFileAttributes” are only supported on file systems that support the Posix family standards. But windows is a dos file system. In this code trying to use PosixFileAttributes on a dos file system causes an “UnsupportedOperationException ” at the runtime. So other options are incorrect.

If this code runs on a unix file system then the code fragment I will produce the expected output.

References: http://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/PosixFileAttributes.html, http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html

 

Domain : Java Stream API

Q25 : Given

  1.     import java.util.stream.IntStream;

  2.     

  3.     public class Whiz{

  4.     

  5.     public static void main(String[] args){

  6.     

  7.                 IntStream ints = IntStream.of(3,2,1,5,3,4,5);                  

  8.                 System.out.print(ints.filter(e -> e>3).distinct().average().getAsDouble());

  9.     }

  10.    }

What is the output?

A. 5.0
B. 4.0
C. 4.5
D. 3.28
E. Compilation fails due to error at line 8

Correct Answer: C

Explanation

First at line 7, we have created an IntStream with duplicated values. At line 8 we have used a filter to get a stream which contains only values greater than 3. Calling distinct will remove all duplicated values in the stream, so finally when average calls only 4 and 5 are remaining. So the result average will be 9 divided by 2 ( = 4.50). Since we use getAsDouble, the whole number with fraction part will be printed, hence option C is correct.

Options A and D are incorrect as it is not the average.
Option B is incorrect as we get the double value.

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#noneMatch-java.util.functon.Predicate-

Summary

Hope we are able to solve your doubts and you have got clarity on some of the concepts of the Oracle Certified Professional [Java SE 8 1Z0-809] certification exam. Learn and explore more such concepts with Oracle Certified Professional [Java SE 8] Practice Tests & Video course available on our official website.

About Pavan Gumaste

Pavan Rao is a programmer / Developer by Profession and Cloud Computing Professional by choice with in-depth knowledge in AWS, Azure, Google Cloud Platform. He helps the organisation figure out what to build, ensure successful delivery, and incorporate user learning to improve the strategy and product further.

LEAVE A REPLY

Please enter your comment!
Please enter your name here