Sample exam questions Sample questions by Oracle

1Z0-809 (OCPJP 8) sample exam questions by Oracle

1z0-809 sample exam questions
Written by Mushfiq Mammadov

1. Given:

public abstract class Customer {
[nbsp count=4]private String name;
[nbsp count=4]public Customer (String name) {
[nbsp count=8]this.name = name;
[nbsp count=4]}
[nbsp count=4]public String getName() { return name; }
[nbsp count=4]public abstract void buy();
}

Which two statements are true about Customer?

A) The Customer class cannot be extended.
B) The Customer class cannot be instantiated.
C) Subclasses of Customer cannot override getName() method.
D) Concrete subclasses of Customer must use a default constructor.
E) Concrete subclasses of Customer must implement the buy() method.
F) Subclasses of Customer must implement the buy() method.

 

2. Given:

class Toy {
[nbsp count=4]double price;
[nbsp count=4]String color;
[nbsp count=4]Toy(String color, double price) {
[nbsp count=8]this.color = color;
[nbsp count=8]this.price = price;
[nbsp count=4]}
[nbsp count=4]public double getPrice() {
[nbsp count=8]return price;
[nbsp count=4]}
[nbsp count=4]public String getColor() {
[nbsp count=8]return color;
[nbsp count=4]}
}

And given the code fragment:

List<Toy> toys = new ArrayList<>();
toys.add(new Toy("red", 10));
toys.add(new Toy("yellow", 10));
toys.add(new Toy("red", 10));
double totalPrice = toys.stream()
[nbsp count=4].filter(e -> e.getColor() == "red")
[nbsp count=4]/* Line n1 */
[nbsp count=4].sum();
System.out.println("Total Price of Red Toys: " + totalPrice);

Which code fragment can be inserted at Line n1 to enable the code to print Total Price of Red Toys: 20.0?

A) .flatMap(e -> e.getPrice())
B) .mapToDouble(e -> e.getPrice())
C) .map(e -> e.getPrice())
D) .peek(e -> e.getPrice())

 

3. Given the code fragment:

class MyResource1 implements AutoCloseable {
[nbsp count=4]public void close() throws IOException {
[nbsp count=8]System.out.print("1 ");
[nbsp count=4]}
}
class MyResource2 implements Closeable {
[nbsp count=4]public void close() throws IOException {
[nbsp count=8]throw new IOException();
[nbsp count=4]}
}
public class TestRes {
[nbsp count=4]public static void main(String[] args) {
[nbsp count=8]try (MyResource1 r1 = new MyResource1();
[nbsp count=15]MyResource2 r2 = new MyResource2();) {
[nbsp count=12]System.out.print("T "); }
[nbsp count=8]catch (IOException ioe) {
[nbsp count=12]System.out.print("IOE ");
[nbsp count=8]} finally {
[nbsp count=12]System.out.print("F ");
[nbsp count=8]}
[nbsp count=4]}
}

What is the result?

A) T 1 IOE F
B) T IOE F
C) T IOE 1 F
D) Compilation fails.

 

4. Assuming that the TestResult.txt file exists and given the code fragment:

public class TestReadFile {
[nbsp count=4]public void readFile(String fName) throws IOException {
[nbsp count=8]// Line n1
[nbsp count=8]Stream<String> content = Files.lines(p);
[nbsp count=8]content.forEach(s1 -> System.out.println(s1));
[nbsp count=4]}
[nbsp count=4]public static void main(String[] args) throws IOException {
[nbsp count=8]TestReadFile trf = new TestReadFile();
[nbsp count=8]trf.readFile("TestResult.txt ");
[nbsp count=4]}
}

Which code fragment at Line n1 compiles?

A) Path p = new Path(fName);
B) Path p = Paths.get(fName);
C) Path p = Paths.toPath(fName);
D) Path p = Paths.get(new File(fName));

 

5. Which class definition compiles?

A) class CallerThread1 implements Callable<String> {
[nbsp count=7]public String call() throws Exception { return "thread";}
[nbsp count=3]}

B) class CallerThread2 implements Callable {
[nbsp count=7]public void call() {}
[nbsp count=3]}

C) class CallerThread3 extends Callable {
[nbsp count=7]public void call() throws IOException {}
[nbsp count=3]}

D) class CallerThread4 implements Callable<String> {
[nbsp count=7]public String call(String s) { return "thread";}
[nbsp count=3]}

E) class CallerThread5 extends Callable<String> {
[nbsp count=7]public void callable(String s) throws Exception {}
[nbsp count=3]}

 

6. Given the code fragment:

[nbsp count=7]Queue<String> products = new ArrayDeque<String>();
[nbsp count=4]products.add("p1");
[nbsp count=4]products.add("p2");
[nbsp count=4]products.add("p3");
[nbsp count=4]System.out.print(products.peek());
[nbsp count=4]System.out.print(products.poll());
[nbsp count=4]System.out.println("");
[nbsp count=4]products.forEach(s -> System.out.print(s));

What is the result?

A. p1p1
[nbsp count=4]p2p3
B. p1p2
[nbsp count=4]p1p2p3
C. p1p2
[nbsp count=4]p3
D. p1p1
[nbsp count=4]p1p2p3

 

7. Given the code fragment:

try (Connection con = DriverManager.getConnection(url, uname, pwd)) {
[nbsp count=4]Statement stmt = con.createStatement();
[nbsp count=4]System.out.print(stmt.executeUpdate("INSERT INTO Emp VALUES (500,'Murray')"));
}

Assuming the SQL query executes properly, what is the result?

A) true
B) false
C) 1
D) 0

 

8. Given the code fragment:

public class TestFun {
[nbsp count=4]public static void main(String[] args) {
[nbsp count=8]List<Integer> nums = Arrays.asList(1,2,3,4,5);
[nbsp count=8]// Line n1
[nbsp count=4]}
}

Which code fragment can be inserted at Line n1 to enable the code to print 2 4?

A) nums.peek(n -> n%2 == 0)
[nbsp count=7].forEach( s -> System.out.print(" "+s));

B) nums.filter(n -> n%2 == 0)
[nbsp count=7].forEach( s -> System.out.print(" "+s));

C) nums.map(n -> n%2 == 0)
[nbsp count=7].forEach( s -> System.out.print(" "+s));

D) nums.stream()
[nbsp count=7].filter(n -> n%2 == 0)
[nbsp count=7].forEach( s -> System.out.print(" "+s));

 

Answers:

1. B and E
2. B
3. A
4. B
5. A
6. A
7. C
8. D

 

Source: Oracle, Exam 809: Java SE 8

About the author

Mushfiq Mammadov

1 Comment

  • Hi Mushfiq,

    Thanks for sharing. Its very helpful. I have also given the exam, I remember there was a question in which one a same statement 2 times executequery was executed. If you know or remember what will happen in that case?

    Thanks
    Saurabh

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.