Sei sulla pagina 1di 26

1Z0-809

PDF

Oracle 1Z0-809 PDF is available.


Enrolling now you will get access to 340 unique 1Z0-809 certification
questions at:
https://www.certification-questions.com/java8-free-pdf-download/1Z0-809-pdf.html

This is a sample demo for 1Z0-809:


Q1. Consider the following class.

1. public final class Program {


2.
3. final private String name;
4.
5. Program (String name){
6. this.name = name;
7.
8. getName();
9. }
10.
11. //code here
12. }

Which of the following code 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.

https://www.certification-questions.com/

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 : Encapsulation and Subclassing - Making classes immutable.

Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html

https://www.certification-questions.com/

Q2. Consider the following code:

1. class SuperClass{
2. protected void method1(){
3. System.out.print("M SuperC");
4. }
5. }
6.
7. class SubClass extends SuperClass{
8. private void method1(){
9. System.out.print("M SubC");
10. }
11.
12. public static void main(String[] args){
13. SubClass sc = new SubClass();
14. sc.method1();
15. }
16. }

What will be the result?

F. M SubC.

G. M SuperC.

H. M SuperCM SubC.

I. Compilation fails.

None of above.

Option D is correct.

The code fails to compile at line 8, cannot reduce the visibility of the inherited method from SuperClass.

Exam objective : Encapsulation and Subclassing - Creating and use Java subclasses.

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

https://www.certification-questions.com/

Q3. Given the following class:

1. public class Test {


2. public static void main(String args[]) {
3. //Code Here
4. Thread thread = new Thread(r);
5. thread.start();
6. }
7. }

Which of the following lines will give a valid Thread creation:

A. Thread r = () -> System.out.println("Running");

B. Run r = () -> System.out.println("Running");

C. Runnable r = () -> System.out.println("Running");

D. Executable r = () -> System.out.println("Running");

E. None Of Above

Option C is correct.

Option A,B, and D are incorrect they are not functional interface, so C is the only valid option.

Exam objective : Concurrency - Creating worker threads using Runnable and Callable.

Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/

https://www.certification-questions.com/

Q4 which of the following database url 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 : Database Applications with JDBC - Connecting to a database by using a JDBC driver.

Oracle Reference : https://docs.oracle.com/javase/tutorial/jdbc/overview/

https://www.certification-questions.com/

Q5. Given the following code:

1. public class Program {


2.
3. public static void main (String args[]) throws IOException {
4. Console c = System.console();
5. int i = (int)c.readLine("Enter value: ");
6. for (int j = 0; j < i; j++) {
7. c.format(" %2d",j);
8. }
9. }
10. }

What will be the result entering the values 5?

F. 1 2 3 4 5

A. 0 1 2 3 4

B. 0 2 4 6 8

C. The code will not compile because of line 5.

D. Unhandled exception type NumberFormatException al line 7.

Option D is correct.

The method signature "int readLine(String fmt, Object... args)" doesn't exist,

the right method return a String so line 5 will give a compile error and option D is right.

Exam objective : I/O Fundamentals - Read and write data from the console

Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/io/cl.html

https://www.certification-questions.com/

Q6. Given

11. class Singleton {


12. private int count = 0;
13. private Singleton(){};
14. public static final Singleton getInstance(){ return new Singleton(); };
15. public void add(int i){ count+=i; };
16. public int getCount(){ return count;};
17. }
18.
19. public class Program {
20. public static void main(String[] args) {
21. Singleton s1 = Singleton.getInstance();
22. s1.add(3);
23. Singleton s2 = Singleton.getInstance();
24. s2.add(2);
25. Singleton s3 = Singleton.getInstance();
26. s2.add(1);
27. System.out.println(s1.getCount()+s2.getCount()+s3.getCount());
28. }
29. }

What will be the result?

A. 18

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 , infact at each method invocation "getInstance()" a new object is
created, and so s1, s2, s3 count instance variable are 3, 2, 1 and then option C is correct.

Exam objective : Java Class Design - Create and use singleton classes and immutable classes

Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html

https://www.certification-questions.com/

Q7. Given

1. public class Program {


2.
3. public static void main(String[] args) {
4.
5. List<Integer> list = Arrays.asList(4,6,12,66,3);
6.
7. String s = list.stream().map( i -> {
8. return ""+(i+1);
9. }).reduce("", String::concat);
10.
11. System.out.println(s);
12. }
13. }

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
return a new String with value i+1. The stream is then reduced to a String by the concatenation function
"String::concat". So Option B is correct, and A ,C, D, E are incorrect.

Exam objective : Collections Streams, and Filters - Iterating through a collection using lambda syntax

Oracle Reference : https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

https://www.certification-questions.com/

Q8.Which of the following are correct override of Object class

I. public int hashCode();


II. public String toString();
III. public boolean equals(Object obj);
IV. public Class getClass();

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 method signature specified above so the override is possible on all options less IV
because is declared final in Object class, so B is correct.

Exam objective : Java Class Design - Override hashCode, equals, and toString methods from Object class

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

https://www.certification-questions.com/

Q9. Given

1. public class Test {


2. public static <T> int count(T[] array, T elem) {
3. int count = 0;
4. for (T e : array)
5. if( e.compareTo(elem) > 0) ++count;
6.
7. return count;
8. }
9. public static void main(String[] args) {
10. Integer[] a = {1,2,3,4,5};
11. int n = Test.<Integer>count(a, 3);
12. System.out.println(n);
13. }
14. }

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",
to make it compile line 2 need to be changed in :

public static <T extends Comparable<T>> int count(T[] array, T elem) {

Exam objective : Collections and Generics - Creating a custom generic class

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

https://www.certification-questions.com/

Q10. Given

1. public class Program {


2. public static void main(String[] args) {
3.
4. Thread th = new Thread(new Runnable(){
5.
6. static {
7. System.out.println("initial");
8. }
9.
10. @Override
11. public void run() {
12. System.out.println("start");
13. }
14. });
15.
16. th.start();
17. }
18. }

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 fail at line 6, so E is correct and
A, B, C, D incorrect.

Exam objective : Interfaces and Lambda Expressions - Anonymous inner classes

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

https://www.certification-questions.com/

Q11. Consider the following class.

1. final class A{
2. private String s;
3. public A(String s){
4. this.s = s;
5. }
6. public String toString(){ return s; };
7. public void setA(String a){ this.s+= a; };
8. }
9.
10. public final class Immutable {
11. private final A a;
12. public Immutable(A a){
13. this.a = a;
14. }
15. public String toString(){ return a.toString();};
16. public static void main(String[] args){
17.
18. A a = new A("Bye");
19. Immutable im = new Immutable(a);
20. System.out.print(im);
21.
22. a.setA(" bye");
23. System.out.print(a);
24. }
25. }

What will be the result?

A. Bye bye

B. Bye Bye

C. ByeBye bye

D. Compilation fail

E. None of Above

Option B is correct.

The object "im" is not really an immutable object. The class Don't provide "setter" methods, field is final and private,
don't allow subclasses to override methods, unfortunately in constructur Object a is mutable and pass by reference
without make a protection copy of the object.

Exam objective : Encapsulation and Subclassing - Making classes immutable.

Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html


https://www.certification-questions.com/

Q12. Given

1. // Code Here
2. @Override
3. public void run() {
4. for(int i = 0;i<10;i++)
5. System.out.print(i);
6. }
7. }
8.
9. public class Test {
10. public static void main(String args[]) {
11. Task t = new Task();
12. Thread thread = new Thread(t);
13. thread.start();
14. }
15. }

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 : Concurrency - Creating worker threads using Runnable and Callable.

Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/

https://www.certification-questions.com/

Q13. Given

1. public class Program {


2.
3. public static void main(String[] args){
4.
5. Callable<String> c = new Callable<String>(){
6. @Override
7. public String call() throws Exception {
8. String s="";
9. for (int i = 0; i < 10; i++) { s+=i;}
10. return s;
11. }
12. };
13.
14. ExecutorService executor = Executors.newSingleThreadExecutor();
15. Future<String> future = executor.submit(c);
16. try {
17. String result = future.wait();
18. System.out.println(result);
19. } catch (ExecutionException e) {
20. e.printStackTrace();
21. }
22. }
23. }

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, syntax is correct so option C is incorrect. Passing
the object "c" to an executor and 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 : Concurrency - Creating worker threads using Runnable and Callable.

Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/

https://www.certification-questions.com/

Q14. Which of the followings are valid Executors factory methods:

I. ExecutorService es1 = Executors.newSingleThreadExecutor(4);


II. ExecutorService es1 = Executors.newFixedThreadPool(10);
III. ExecutorService es1 = Executors.newScheduledThreadPool();
IV. ExecutorService es1 = Executors.newScheduledThreadPool(10);
V. ExecutorService es1 = Executors.newSingleThreadScheduledExecutor();

A. I, II, III

B. II, III, IV, V

C. I, IV, V

D. II, IV, IV

E. All

Option D is correct.

The method "newSingleThreadExecutor" cannot accept parameters I is incorrect, the method


"newScheduledThreadPool" with parameters doesn't exists so III is incorrect.

Exam objective : Concurrency - Using an ExecutorService to concurrently execute tasks.

Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/

https://www.certification-questions.com/

Q15. Given

1. class A {
2. private String s;
3. public A(String in){ this.s = in;}
4.
5. public boolean equals(Object in){
6. if(getClass() != in.getClass()) return false;
7. A a = (A)in;
8. boolean ret = (this.s.equals(a.s));
9. return ret;
10. }
11. };
12.
13. public class Program {
14. public static void main(String[] args) {
15. A a1 = new A("A");
16. A a2 = new A("A");
17. if(a1 == a2) System.out.println("true");
18. else System.out.println("false");
19. }
20. }

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 is been override in a correct way, anyway the operator "==" will continue to compare
object reference, so option B is correct.

Exam objective : Java Class Design - Override hashCode, equals, and toString methods from Object class

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

https://www.certification-questions.com/

Q16. Given the class definition

1. class G<T> {
2. private T t;
3.
4. public void set(T t) { this.t = t; }
5. public T get() { return t; }
6. }

Which of the following are valid G Class instantiation.

I. G<String> gen = new G<String>();


II. G<> gen = new G<>();
III. G gen = new G();
IV. G<A<String>> gen = new G<A<String>>();
V. G<int> gen = new G<int>();

F. I, II, III.

G. I, II, IV.

H. I, III.

I. I, III, IV.

J. All.

Option D is correct.

A type variable can be any non-primitive type and another type variable, so option I,IV are correct and V incorrect,
option II is incorrect because miss the type variable, option III is correct because is the raw type of G<T>.

Exam objective : Collections and Generics - Creating a custom generic class

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

https://www.certification-questions.com/

Q17. Given

1. class Person{
2. private String name;
3. private int age;
4. Person(String name, int age){
5. this.name = name;
6. this.age = age;
7. }
8. public String toString(){ return name + " " + age; }
9.
10. public static Comparator<Person> COMPARATOR = new Comparator<Person>() {
11. public int compare(Person o1, Person o2) {
12. return (o1.age - o2.age);
13. }
14. };
15. }
16. public class Program {
17.
18. public static void main(String[] args) {
19. List<Person> list = new ArrayList<Person>();
20. list.add(new Person("John",50));
21. list.add(new Person("Frank",15));
22. list.add(new Person("Adam",20));
23.
24. Collections.sort(list,Person.COMPARATOR);
25. list.forEach(a -> System.out.print(a.toString()+" "));
26. }
27. }

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.

Option C is correct.

With the use of The Comparator Object defined in line 10 we sort objects in an order other than their natural
ordering, that is the age instance attribute, than option C is correct meanwhile A and B incorrect. The code has no
compile error and no exception will throw so option D e E are incorrect.

Exam objective : Collections and Generics - Ordering collections

Oracle Reference : https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html


https://www.certification-questions.com/

Q18. Given the following code

1. interface A {
2. public void m();
3. };
4. public class Program {
5. public static void main(String[] args) {
6. int y = 0;
7. //Code Here
8. }
9. }

What are valid anonymous class declaration:

I. new A(){
void m1(){};
public void m(){};
};
II. new A(){
static int x;
public void m(){};
};
III. new A(){
public void m(){ y++; };
};
IV. new A(){
static final int x = 0;
public void m(){};
};

F. I, II, III.

G. I, IV.

H. I, II.

I. II, IV.

J. All.

Option B is correct.

An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively
final, so III is incorrect. An anonymous class can have static members provided that they are constant variables, then
II is incorrect. Option I and IV are valid declaration, so B is correct.

Exam objective : Interfaces and Lambda Expressions - Anonymous inner classes

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

https://www.certification-questions.com/

Q19. Given

1. class Person{
2. private String name;
3. private int age;
4. Person(String name, int age){
5. this.name = name;
6. this.age = age;
7. }
8.
9. public String toString(){ return name + " " + age; }
10.
11. public static Comparator<Person> COMPARATOR = new Comparator<Person>() {
12. public int compare(Person o1, Person o2) {
13. return (o1.age - o2.age);
14. };
15. };
16. };
17. public class Program {
18.
19. public static void main(String[] args) {
20. Set<Person> list = new TreeSet<Person>(Person.COMPARATOR);
21. list.add(new Person("John",50));
22. list.add(new Person("Frank",15));
23. list.add(new Person("Adam",15));
24. list.forEach(a -> System.out.print(a.toString()+" "));
25. }
26. }

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
we are comparing only age attribute and than Option A is correct and B and C are incorrect. The code has no compile
error and no exception will throw so option D e E are incorrect.

Exam objective : Collections and Generics - Ordering collections

Oracle Reference : https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html


https://www.certification-questions.com/

Q20.Given

1. public class Program {


2.
3. public static void main(String[] args){
4. String url = "jdbc:derby:testdb;create=true";
5. try{
6. Connection conn = DriverManager.getConnection(url);
7.
8. String query = "select name from contacts";
9. Statement stmt = conn.createStatement();
10.
11. ResultSet rs = stmt.execute(query);
12. while (rs.next()) {
13. String name = rs.getString("Name");
14. System.out.println(name);
15. }
16. }catch(SQLException e){
17. System.out.println(e.getMessage());
18. }
19. }
20. }

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" doesn't return a ResultSet but true if the first
object that the query returns is a ResultSet object, to make the code compile the right method is "executeQuery".

Exam objective : Database Applications with JDBC - Submitting queries and get results from the database.

Oracle Reference : https://docs.oracle.com/javase/tutorial/jdbc/overview/

https://www.certification-questions.com/

Q21. Given

1. public class Program {


2. public static <T> int count(T[] array) {
3. int count = 0;
4. for (T e : array) ++count;
5. return count;
6. }
7. public static void main(String[] args) {
8. Integer[] a = {1,2,3,4,5};
9. //Code here
10. System.out.println(n);
11. }
12. }

which of the following are valid method's 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 syntax for generic method invocation, option B is the correct syntax for generic method
invocation, type inference allows you to invoke a generic method as an ordinary method so option D is correct as
well.

Exam objective : Collections and Generics - Creating a custom generic class

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

https://www.certification-questions.com/

Q22.Which of the following Statement are true

I. An anonymous class has access to the members of its enclosing class.

II. An anonymous class cannot access local variables in its enclosing scope that are not

declared as final or effectively final.

III. You cannot declare static initializers or member interfaces.

IV. An anonymous class can have static members provided that they are constant variables.

A. I, II, III.

B. I, II.

C. II, III, IV.

D. III, IV.

E. All.

Option E is correct.

Exam objective : Interfaces and Lambda Expressions - Anonymous inner classes

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

https://www.certification-questions.com/

Q23. Given

1. class A {
2. private String s;
3. public A(String s){ this.s = s;};
4. public String toString(){ return s;}
5. }
6. public class Program {
7.
8. public static void main(String[] args) {
9.
10. List<A> list = new ArrayList<A>();
11. list.add(new A("flower"));
12. list.add(new A("cat"));
13. list.add(new A("house"));
14. list.add(new A("dog"));
15. Collections.sort(list);
16. list.forEach(a -> System.out.print(a.toString()+" "));
17. }
18. }

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, the Collections "sort" method has signature as following:

Collectios <T extends Comparable<? super T>> void sort(List<T> list)

So the code will not compile because class A doesn't implement Comparable interface, and than D

is correct and A,B,C and E are incorrect.

Exam objective : Collections and Generics - Ordering collections

Oracle Reference : https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

https://www.certification-questions.com/

Q24. Which of those are valid Standard Streams

I. System.in

II. System.error

III. System.out

IV. System.input

V. System.output

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 syntax, so the correct answer is D.

Exam objective : I/O Fundamentals - Read and write data from the console

Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/io/cl.html

https://www.certification-questions.com/

Q25. Which is/are true?

I. In FileInputStream and FileOutputStream each read or write request is handled directly

by the underlying OS.

II. Programs use byte streams to perform input and output of 8-bit bytes.

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.

A. I, II, III, IV.

B. II, III.

C. II, III, IV.

D. III, IV.

E. All.

Option E is correct.

Exam objective : I/O Fundamentals - Using streams to read and write files

Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/io

https://www.certification-questions.com/

Potrebbero piacerti anche