Java 1Z0-809 Free Mock Exam

Access our sample Oracle 1Z0-809 mock exam for free.
There is no registration, no subscription, no money involved.

WE VALUE YOUR SATISFACTION!

DOWNLOAD FREE PDF TAKE A FREE TEST BUY FULL MOCK
Oracle 1Z0-809 Free Mock Exam


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



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?

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	: Encapsulation and Subclassing - Creating and use Java subclasses.
Oracle Reference : https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html



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



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	: Database Applications with JDBC - Connecting to a database by using a JDBC driver.
Oracle Reference : https://docs.oracle.com/javase/tutorial/jdbc/overview/



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 of entering the value 5?

A. 1 2 3 4 5
B. 0 1 2 3 4
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	: I/O Fundamentals - Read and write data from the console
Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/io/cl.html



Q6. 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;};
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?

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


Q7. Given the following class:

1.	public class Program {
2.	
3.		public static void main(String[] args) {
4.		
5.			List 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 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



Q8. Which of the following are correct overrides 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 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	: Java Class Design - Override hashCode, equals, and toString methods from Object class
Oracle Reference : https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html



Q9. Consider the following class:

1.	public class Test {
2.		public static  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.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". In order to make it compile line 2 needs to be changed in:

public static > 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



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



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(im);
24.		}
25.	}

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


Q12. 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);
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/



Q13. Consider the following class:

1.	public class Program {
2.	
3.		public static void main(String[] args){
4.		
5.			Callable c = new Callable(){
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 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, 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/



Q14. 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();

A. I, II, III
B. II, III, IV, V
C. I, IV, V
D. II, IV, V
E. All

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	: Concurrency - Using an ExecutorService to concurrently execute tasks.
Oracle Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/



Q15. Given the following class:

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 has been overriden in a correct way, but 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



Q16. Given the class definition:

1.	class G {
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 instantiations?

I.	G gen = new G();
II.	G<> gen = new G<>();
III.    G gen = new G();
IV.	G> gen = new G>();
V.	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.

Exam Objective	: 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.	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 COMPARATOR = new Comparator() {
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 list = new ArrayList();
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 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.		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(){}; 
		};
		
A. I, II, III.
B. I, IV.
C. I, II.
D. II, IV.
E. All.

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	: 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.	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 COMPARATOR = new Comparator() {
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 list = new TreeSet(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 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	: Collections and Generics - Ordering collections
Oracle Reference : https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html



Q20. Given the following class:

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" 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 Applications with JDBC - Submitting queries and get results from the database.
Oracle Reference : https://docs.oracle.com/javase/tutorial/jdbc/overview/



Q21. Consider the following class:

public class Program {
	public static  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 = Program.count(a);
B. int n = Program.count(a);
C. int n = Program.count(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	: Collections and Generics - Creating a custom generic class
Oracle Reference : https://docs.oracle.com/javase/tutorial/java/generics/methods.html



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



Q23. Given the following class:

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 list = new ArrayList();
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, and the Collections "sort" method has the following signature:
 
Collections > void sort(List 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	: Collections and Generics - Ordering collections
Oracle Reference : https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html



Q24. Which of the following 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 syntaxes, 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



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