You are on page 1of 10

Java 1Z0-809

Java SE 8 Programmer II
Java 1Z0-809 Dumps Available Here at:
https://www.certification-questions.com/java-exam/1z0-809-dumps.html

Enrolling now you will get access to 425 questions in a unique set of 1Z0-
809 dumps

Question 1
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?

Options:

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.

Answer: A, C

Explanation:
D have a compile error since name variable is final.
Option C is private and doesn't change the name value.

https://www.certification-questions.com
Java 1Z0-809

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

Question 2
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?

Options:

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

Answer: C

Explanation:
nd D are incorrect as they are not functional interfaces, 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/

Question 3
Given the following class:

1. class Singleton {
2. private int count = 0;
3. private Singleton(){};
4. public static final Singleton getInstance(){ return new Singleton(); };
5. public void add(int i){ count+=i; };
6. public int getCount(){ return count;};

https://www.certification-questions.com
Java 1Z0-809

7. }
8.
9. public class Program {
10. public static void main(String[] args) {
11. Singleton s1 = Singleton.getInstance();
12. s1.add(3);
13. Singleton s2 = Singleton.getInstance();
14. s2.add(2);
15. Singleton s3 = Singleton.getInstance();
16. s2.add(1);
17. System.out.println(s1.getCount()+s2.getCount()+s3.getCount());
18. }
19. }

What will be the result?

Options:

A. 18

B. 7

C. 6

D. The code will not compile.

E. None of above

Answer: C

Explanation:
ngleton" 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 : Java Class Design - Create and use singleton classes and immutable classes
Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html

Question 4
Given the following class:

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 -> {

https://www.certification-questions.com
Java 1Z0-809

8. return ""+(i+1);
9. }).reduce("", String::concat);
10.
11. System.out.println(s);
12. }
13. }

What will be the result?

Options:

A. 4612663

B. 5713674

C. 3661264

D. The code will not compile because of line 7.

E. Unhandled exception type NumberFormatException al line 8.

Answer: B

Explanation:
s 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 : 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

Question 5
Consider the following class:

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);

https://www.certification-questions.com
Java 1Z0-809

13. }
14. }

What will be the result?

Options:

A. 2

B. 3

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

D. An exception is thrown.

E. None of Above.

Answer: C

Explanation:
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 : Collections and Generics - Creating a custom generic class
Oracle Reference : https://docs.oracle.com/javase/tutorial/java/generics/methods.html

Question 6
Given the following class:

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. }

https://www.certification-questions.com
Java 1Z0-809

18. }

What will be the result?

Options:

A. start initial

B. initial start

C. initial

D. A runtime exception is thrown.

E. The code will not compile because of line 6.

Answer: E

Explanation:
annot 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 : Interfaces and Lambda Expressions - Anonymous inner classes
Oracle Reference : https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

Question 7
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);

https://www.certification-questions.com
Java 1Z0-809

20. System.out.print(im);
21.
22. a.setA(" bye");
23. System.out.print(im);
24. }
25. }

What will be the result?

Options:

A. Bye bye

B. Bye Bye

C. ByeBye bye

D. Compilation failure

E. None of Above

Answer: C

Explanation:
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 : Encapsulation and Subclassing - Making classes immutable.
Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html

Question 8
Given the following code:

1. // Code Here
2. @Override
3. public void run() {
4. for(int i = 0;i<10;i++)
5. System.out.print(i);

https://www.certification-questions.com
Java 1Z0-809

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?

Options:

A. class Task extends Runnable {

B. class Task implements Runnable {

C. class Task implements Thread {

D. class Task extends Thread {

E. None Of Above

Answer: B

Explanation:
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/

Question 9
Consider the following class:

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;

https://www.certification-questions.com
Java 1Z0-809

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?

Options:

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.

Answer: E

Explanation:
ng 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 : Concurrency - Creating worker threads using Runnable and Callable.
Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/

Question 10
Which of the following 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();

https://www.certification-questions.com
Java 1Z0-809

Options:

A. I, II, III

B. II, III, IV, V

C. I, IV, V

D. II, IV, V

E. All

Answer: D

Explanation:
ewSingleThreadExecutor" cannot accept parameters so I is incorrect, and
the method "newScheduledThreadPool" with parameters doesn't exist so III is incorrect.
Exam Objective : Concurrency - Using an ExecutorService to concurrently execute tasks.
Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/

Would you like to see more? Don't miss our 1Z0-809 PDF
file at:
https://www.certification-questions.com/java-pdf/1z0-809-pdf.html

https://www.certification-questions.com

You might also like