190 likes | 245 Vues
we need to target www.javamockexams.com with the following
E N D
Free On-Line Java 8 Certification Test Oracle JAVA8 1Z0-809 Free Mock Exam is also available here: https://www.javamockexams.com/java8/1Z0-809/free-test.html Join now to get full access to a set of three 1Z0-809 Mock Exams. Joining now you will get access to 340 unique Java 8 Certification Questions. Visit us at www.javamockexams.com Q1. Consider the following class: public final class Program { final private String name; Program (String name){ this.name = name; getName(); } //code here } Which of the following codes will make an instance of this class immutable? A. public String getName(){return name;} B. public String getName(String value){ name=value; return value;} C. private String getName(){return name+"a";} D. public final String getName(){return name+="a";} E. All of Above. Option A,C are correct. Option B and D have a compile error since name variable is final. Option C is private and doesn't change the name value. Option A is public and doesn't change the name value. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html : Encapsulation and Subclassing - Making classes immutable. Q2. Consider the following code: www.javamockexams.com
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. class SuperClass{ protected void method1(){ System.out.print("M SuperC"); } } class SubClass extends SuperClass{ private void method1(){ System.out.print("M SubC"); } public static void main(String[] args){ SubClass sc = new SubClass(); sc.method1(); } } What will be the result? A. M SubC. B. M SuperC. C. M SuperCM SubC. D. Compilation fails. E. None of above. Option D is correct. The code fails to compile at line 8 as it cannot reduce the visibility of the inherited method from SuperClass. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html : Encapsulation and Subclassing - Creating and use Java subclasses. Q3. Given the following class: 1. 2. 3. 4. 5. 6. 7. public class Test { public static void main(String args[]) { //Code Here Thread thread = new Thread(r); thread.start(); } } www.javamockexams.com
Which of the following lines will give a valid Thread creation? A. B. C. D. E. Thread r = () -> System.out.println("Running"); Run r = () -> System.out.println("Running"); Runnable r = () -> System.out.println("Running"); Executable r = () -> System.out.println("Running"); None Of Above Option C is correct. Option A,B, and D are incorrect as they are not functional interfaces, so C is the only valid option. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/ : Concurrency - Creating worker threads using Runnable and Callable. Q4. Which of the following database urls are correct? A. jdbc:mysql://localhost:3306 B. jdbc:mysql://localhost:3306/sample C. jdbc:mysql://localhost:3306/sample/user=root?password=secret D. jdbc:mysql://localhost:3306/sample?user=root&password=secret E. All Option A,B and D are correct. The correct url format is the following: jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... So C is incorrect and A,B and D are correct. Exam Objective driver. Oracle Reference : https://docs.oracle.com/javase/tutorial/jdbc/overview/ : Database Applications with JDBC - Connecting to a database by using a JDBC Q5. Given the following code: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. public class Program { public static void main (String args[]) throws IOException { Console c = System.console(); int i = (int)c.readLine("Enter value: "); for (int j = 0; j < i; j++) { c.format(" %2d",j); } } } What will be the result of entering the value 5? A. 1 2 3 4 5 B. 0 1 2 3 4 www.javamockexams.com
C. 0 2 4 6 8 D. The code will not compile because of line 5. E. Unhandled exception type NumberFormatException at line 7. Option D is correct. The method's signature "int readLine(String fmt, Object... args)" doesn't exist as the right method's signature returns a String so line 5 will give a compile error and option D is right. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/io/cl.html : I/O Fundamentals - Read and write data from the console Q6. Given the following class: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. class Singleton { private int count = 0; private Singleton(){}; public static final Singleton getInstance(){ return new Singleton(); }; public void add(int i){ count+=i; }; public int getCount(){ return count;}; } public class Program { public static void main(String[] args) { Singleton s1 = Singleton.getInstance(); s1.add(3); Singleton s2 = Singleton.getInstance(); s2.add(2); Singleton s3 = Singleton.getInstance(); s2.add(1); System.out.println(s1.getCount()+s2.getCount()+s3.getCount()); } } What will be the result? A. 18 www.javamockexams.com
B. 7 C. 6 D. The code will not compile. E. None of above Option C is correct. The class "Singleton" is not a real singleton class, in fact at each "getInstance()" method invocation a new object is created, so s1, s2, s3 count instance variable are 3, 2, 1, and then option C is correct. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html : Java Class Design - Create and use singleton classes and immutable classes Q7. Given the following class: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. public class Program { public static void main(String[] args) { List<Integer> list = Arrays.asList(4,6,12,66,3); String s = list.stream().map( i -> { return ""+(i+1); }).reduce("", String::concat); System.out.println(s); } } What will be the result? A. 4612663 B. 5713674 C. 3661264 D. The code will not compile because of line 7. E. Unhandled exception type NumberFormatException al line 8. Option B is correct. The Program is applying a map function to the stream generated from list. For each Integer element "i" the function returns a new String with value i+1. The stream is then reduced to a String by the concatenation "String::concat" function. So Option B is correct, and A ,C, D, E are incorrect. Exam Objective syntax Oracle Reference : https://docs.oracle.com/javase/8/docs/api/java/util/stream/package- www.javamockexams.com : Collections Streams, and Filters - Iterating through a collection using lambda
summary.html Q8. Which of the following are correct overrides of Object class? I. II. III. public boolean equals(Object obj); IV. public Class getClass(); public int hashCode(); public String toString(); A. I, II, III, IV. B. I, II, III. C. I, II. D. III, IV. E. All. Option B is correct. The Object class has all the methods signature specified above so the override is possible on all options except IV because is declared final in Object class, so B is correct. Exam Objective class Oracle Reference : https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html : Java Class Design - Override hashCode, equals, and toString methods from Object Q9. Consider the following class: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. www.javamockexams.com public class Test { public static <T> int count(T[] array, T elem) { int count = 0; for (T e : array) if( e.compareTo(elem) > 0) ++count; } } public static void main(String[] args) { Integer[] a = {1,2,3,4,5}; int n = Test.<Integer>count(a, 3); System.out.println(n); } return count;
What will be the result? A. 2 B. 3 C. The code will not compile because of line 5. D. An exception is thrown. E. None of Above. Option C is correct. C is correct because the variable "e" is a generic "T" type so the compile has no knowledge of method "compareTo". In order to make it compile line 2 needs to be changed in: public static <T extends Comparable<T>> int count(T[] array, T elem) { Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/java/generics/methods.html : Collections and Generics - Creating a custom generic class Q10. Given the following class: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. public class Program { public static void main(String[] args) { Thread th = new Thread(new Runnable(){ static { System.out.println("initial"); } @Override public void run() { System.out.println("start"); } }); th.start(); } } What will be the result? A. start initial B. initial start C. initial D. A runtime exception is thrown. E. The code will not compile because of line 6. Option E is correct. Because you cannot declare static initializers in an anonymous class, the compilation fails at line 6, so E is correct and A, B, C, D are incorrect. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html : Interfaces and Lambda Expressions - Anonymous inner classes www.javamockexams.com
Q11. Consider the following class: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. final class A{ } private String s; public A(String s){ this.s = s; } public String toString(){ return s; }; public void setA(String a){ this.s+= a; }; public final class Immutable { private final A a; public Immutable(A a){ this.a = a; } public String toString(){ return a.toString();}; public static void main(String[] args){ A a = new A("Bye"); Immutable im = new Immutable(a); System.out.print(im); } } a.setA(" bye"); System.out.print(im); What will be the result? A. Bye bye B. Bye Bye C. ByeBye bye D. Compilation failure E. None of Above Option C is correct. In order for the class "immutable" to be an immutable class it needs to satisfy the following four properties: 1.Don't provide "setter" methods - methods that modify fields or objects reffered to by fields. 2. Make all fields final and private. 3. Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods. 4. If the instance fields include references to mutable objects, don't allow those objects to be changed. Properties 1,2,3 are satisfied, but unfortunately the last one is not, so the object "a" is mutable because it is passed by reference without making a protection copy of it. So option C is correct and A,B,D,E are incorrect. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html www.javamockexams.com : Encapsulation and Subclassing - Making classes immutable.
Q12. Given the following code: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. // Code Here @Override public void run() { for(int i = 0;i<10;i++) } } System.out.print(i); public class Test { public static void main(String args[]) { Task t = new Task(); Thread thread = new Thread(t); thread.start(); } } Which of the following lines will give the result 0123456789? A. class Task extends Runnable { B. class Task implements Runnable { C. class Task implements Thread { D. class Task extends Thread { E. None Of Above Option B is correct. We can create a Thread by passing an implementation of Runnable to a Thread constructor, so the only correct option is B. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/ : Concurrency - Creating worker threads using Runnable and Callable. Q13. Consider the following class: 1. 2. 3. 4. www.javamockexams.com public class Program { public static void main(String[] args){
5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. } } Callable<String> c = new Callable<String>(){ @Override public String call() throws Exception { String s=""; for (int i = 0; i < 10; i++) { s+=i;} return s; } }; ExecutorService executor = Executors.newSingleThreadExecutor(); Future<String> future = executor.submit(c); try { String result = future.wait(); System.out.println(result); } catch (ExecutionException e) { e.printStackTrace(); } What will be the result? A. 0123456789 B. 12345678910 C. Unhandled exception type InterruptedException al line 17 D. The code will not compile because of line 5. E. The code will not compile because of line 17. Option E is correct. We are creating a Callable object with an anonymous class at line 5, the syntax is correct so option C is incorrect. Passing the object "c" to an executor will get as return type a Future to wait for thread ends. But at line 17 method wait of Class Future doesn't exist so E is correct. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/ : Concurrency - Creating worker threads using Runnable and Callable. Q14. Which of the following are valid Executors factory methods? I. II. III. IV. V. ExecutorService es1 = Executors.newScheduledThreadPool(); ExecutorService es1 = Executors.newScheduledThreadPool(10); ExecutorService es1 = Executors.newSingleThreadScheduledExecutor(); ExecutorService es1 = Executors.newSingleThreadExecutor(4); ExecutorService es1 = Executors.newFixedThreadPool(10); A. I, II, III B. II, III, IV, V C. I, IV, V D. II, IV, V E. All www.javamockexams.com
Option D is correct. The method "newSingleThreadExecutor" cannot accept parameters so I is incorrect, and the method "newScheduledThreadPool" with parameters doesn't exist so III is incorrect. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/ : Concurrency - Using an ExecutorService to concurrently execute tasks. Q15. Given the following class: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. www.javamockexams.com class A { }; private String s; public A(String in){ this.s = in;} public boolean equals(Object in){ if(getClass() != in.getClass()) return false; A a = (A)in; boolean ret = (this.s.equals(a.s)); return ret; } public class Program { public static void main(String[] args) { A a1 = new A("A"); A a2 = new A("A"); if(a1 == a2) System.out.println("true"); else System.out.println("false"); } }
What will be the result? A. true B. false C. The code will not compile because of line 17. D. Unhandled exception type ClassCastException al line 7. E. None of above Option B is correct. In class A the method equals has been overriden in a correct way, but the operator "==" will continue to compare object reference, so option B is correct. Exam Objective class Oracle Reference : https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html : Java Class Design - Override hashCode, equals, and toString methods from Object Q16. Given the class definition: 1. 2. 3. 4. 5. 6. class G<T> { private T t; } public void set(T t) { this.t = t; } public T get() { return t; } Which of the following are valid G Class instantiations? I. II. III. G gen = new G(); IV. G<A<String>> gen = new G<A<String>>(); V. G<int> gen = new G<int>(); G<String> gen = new G<String>(); G<> gen = new G<>(); A. I, II, III. B. I, II, IV. C. I, III. D. I, III, IV. E. All. Option D is correct. A type variable can be any non-primitive type or a different type variable, so option I,IV are correct and V is incorrect. Option II is incorrect because it misses the type variable, so option III is correct because it is the raw type of G<T>. Exam Objective www.javamockexams.com : Collections and Generics - Creating a custom generic class
Oracle Reference : https://docs.oracle.com/javase/tutorial/java/generics/types.html Q17. Given the following class: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. class Person{ } public class Program { public static void main(String[] args) { List<Person> list = new ArrayList<Person>(); list.add(new Person("John",50)); list.add(new Person("Frank",15)); list.add(new Person("Adam",20)); Collections.sort(list,Person.COMPARATOR); list.forEach(a -> System.out.print(a.toString()+" ")); } } private String name; private int age; Person(String name, int age){ this.name = name; this.age = age; } public String toString(){ return name + " " + age; } public static Comparator<Person> COMPARATOR = new Comparator<Person>() { public int compare(Person o1, Person o2) { return (o1.age - o2.age); } }; What will be the result? A. Adam 20 Frank 15 John 50 B. John 50 Adam 20 Frank 15 C. Frank 15 Adam 20 John 50 D. The code will not compile because of line 24. E. Unhandled exception type ClassCastException al line 24. www.javamockexams.com
Option C is correct. With the use of The Comparator Object defined in line 10 we sort objects in a different order than their natural ordering, that is the age instance attribute, so option C is correct while A and B are incorrect. The code has no compile errors and no exceptions will be thrown, so option D and E are incorrect. Exam Objective : Collections and Generics - Ordering collections Oracle Reference : https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html Q18. Consider the following code: interface A { }; public void m(); public class Program { public static void main(String[] args) { int y = 0; //Code Here } } Which of the following are valid anonymous class declarations? I. II. III. IV. A. I, II, III. B. I, IV. C. I, II. D. II, IV. E. All. new A(){ new A(){ new A(){ }; void m1(){}; public void m(){}; }; static int x; public void m(){}; }; new A(){ }; public void m(){ y++; }; static final int x = 0; public void m(){}; Option B is correct. An anonymous class cannot access local variables that are not declared as final or effectively final in its enclosing scope, so III is incorrect. An anonymous class can have static members provided that are constant variables, so II is incorrect. Option I and IV are valid declarations, so B is correct. Exam Objective www.javamockexams.com : Interfaces and Lambda Expressions - Anonymous inner classes
Oracle Reference : https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html Q19. Given the following class: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. class Person{ }; public class Program { public static void main(String[] args) { Set<Person> list = new TreeSet<Person>(Person.COMPARATOR); list.add(new Person("John",50)); list.add(new Person("Frank",15)); list.add(new Person("Adam",15)); list.forEach(a -> System.out.print(a.toString()+" ")); } } private String name; private int age; Person(String name, int age){ this.name = name; this.age = age; } public String toString(){ return name + " " + age; } public static Comparator<Person> COMPARATOR = new Comparator<Person>() { public int compare(Person o1, Person o2) { return (o1.age - o2.age); }; }; What will be the result? A. Frank 15 John 50 B. John 50 Adam 20 Frank 15 C. Frank 15 Adam 20 John 50 D. The code will not compile because of line 20. E. Unhandled exception type ClassCastException al line 20. Option A is correct. TreeSet is a SortSet so we need to pass a Comparator object in the constructor or make the object Person Comparable. The Set collection doesn't allow duplicate keys and method "compare" is used for object equality, but since we are comparing only age attribute then Option A is correct, and B and C are incorrect. The code has no compile errors and no exceptions will be thrown, so option D and E are incorrect. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html : Collections and Generics - Ordering collections www.javamockexams.com
Q20. Given the following class: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. public class Program { } public static void main(String[] args){ String url = "jdbc:derby:testdb;create=true"; try{ Connection conn = DriverManager.getConnection(url); String query = "select name from contacts"; Statement stmt = conn.createStatement(); ResultSet rs = stmt.execute(query); while (rs.next()) { String name = rs.getString("Name"); System.out.println(name); } }catch(SQLException e){ System.out.println(e.getMessage()); } } What will be the result? A. The code will not compile because of line 11. B. The program will run successful. C. The code will not compile because of line 9. D. An exception is thrown. E. None of Above. Option A is correct. Option A is correct as the code fails to compile. The method "execute" does not return a ResultSet, but it returns true if the first object that the query returns is a ResultSet object. In order to make the code compile the right method is "executeQuery". Exam Objective database. Oracle Reference : https://docs.oracle.com/javase/tutorial/jdbc/overview/ : Database Applications with JDBC - Submitting queries and get results from the Q21. Consider the following class: www.javamockexams.com
public class Program { public static <T> int count(T[] array) { int count = 0; for (T e : array) ++count; return count; } public static void main(String[] args) { Integer[] a = {1,2,3,4,5}; //Code here System.out.println(n); } } Which of the following are valid methods invocation? A. int n = <Integer>Program.count(a); B. int n = Program.<Integer>count(a); C. int n = Program.count<Integer>(a); D. int n = Program.count(a); E. All Option B and D are correct. Option A and C are incorrect syntaxes when performing generic method invocation. Option B is the correct syntax when performing generic method invocation, and since type inference allows you to invoke a generic method as an ordinary method then option D is correct as well. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/java/generics/methods.html : Collections and Generics - Creating a custom generic class Q22. Which of the following statements are true? I. II. III. You cannot declare static initializers or member interfaces. IV. An anonymous class can have static members provided that they are constant variables. An anonymous class has access to the members of its enclosing class. An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final. A. I, II, III. B. I, II. C. II, III, IV. D. III, IV. E. All. Option E is correct. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html : Interfaces and Lambda Expressions - Anonymous inner classes Q23. Given the following class: 1. 2. 3. 4. 5. 6. 7. 8. www.javamockexams.com class A { } public class Program { public static void main(String[] args) { private String s; public A(String s){ this.s = s;}; public String toString(){ return s;}
9. 10. 11. 12. 13. 14. 15. 16. 17. 18. } } List<A> list = new ArrayList<A>(); list.add(new A("flower")); list.add(new A("cat")); list.add(new A("house")); list.add(new A("dog")); Collections.sort(list); list.forEach(a -> System.out.print(a.toString()+" ")); What will be the result? A. house flower dog cat B. cat dog flower house C. flower cat house dog D. The code will not compile because of line 15. E. Unhandled exception type ClassCastException al line 15. Option D is correct. Object "list" is a type A ArrayList, and the Collections "sort" method has the following signature: Collections <T extends Comparable<? super T>> void sort(List<T> list) The code will not compile because class A doesn't implement Comparable interface, so D is correct and A,B,C,E are incorrect. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html : Collections and Generics - Ordering collections Q24. Which of the following are valid Standard Streams? I. II. III. System.out IV. System.input V. System.output System.in System.error A. I. B. II, IV, V. C. I, II, III. D. I, III. E. Any of above. Option D is correct. System.error, System.input and System.output are not valid syntaxes, so the correct answer is D. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/io/cl.html www.javamockexams.com : I/O Fundamentals - Read and write data from the console
Q25. Which of the following statements are true? I. II. III. Character stream I/O automatically translates this internal format to and from the local character set. IV. There are two general-purpose byte-to-character "bridge" streams: InputStreamReader and OutputStreamWriter. In FileInputStream and FileOutputStream each read or write request is handled directly by the underlying OS. Programs use byte streams to perform input and output of 8-bit bytes. A. I, II, III, IV. B. II, III. C. II, III, IV. D. III, IV. E. All. Option E is correct. Exam Objective Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/io : I/O Fundamentals - Using streams to read and write files www.javamockexams.com