Sample exam questions Sample questions by Oracle

1Z0-813 (Upgrade to Java SE 8 OCP) sample exam questions by Oracle

1Z0-813 sample exam questions
Written by Mushfiq Mammadov

1. Which code fragment correctly assign a numeric literal?

A) byte b1 = b1011;
B) byte b2 = 1011b;
C) byte b3 = 0b1001;
D) byte b4 = 0xb001;


2. Given the fragment:

public class MathFun {
[nbsp count=4]public static void main(String[] args) {
[nbsp count=8]int number1 = 0b0111;
[nbsp count=8]int number2 = 0111_000;
[nbsp count=8]System.out.println("Number1: " + number1);
[nbsp count=8]System.out.println("Number2: " + number1);
[nbsp count=4]}

What is the result?

A) Number1: 7
[nbsp count=2]Number2: 7

B) Number1: 7
[nbsp count=2]Number2: 111_000

C) Number1: 0b0111
[nbsp count=2]Number2: 0111000

D) Compilation fails.


3. Given:

The test1.txt file is available and the test2.txt file is not available.
And, given the code fragment:

Path sPath = Paths.get("test1.txt");
Path dPath = Paths.get("test2.txt");
try {
[nbsp count=4]Files.move(sPath, dPath, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException ex) {
[nbsp count=4]System.err.println("Exception!");

Which statement is true?

A) The test1.txt file is renamed to the test2.txt file and the test1.txt file is removed in a single operation.
B) The test1.txt file is renamed to the test2.txt file and the test1.txt file is removed in two distinct operation.
C) The test1.txt file is copied and renamed to the test2.txt file in a single operation.
D) The program prints: Exception!


4. Which is a valid functional interface?

A) public interface Useful<E> {
[nbsp count=6]E getStuff();
[nbsp count=6]void putStuff(E e);
[nbsp count=2]}

B) public interface Useful {
[nbsp count=6]void doStuff();
[nbsp count=6]default void doOtherStuff() {}
[nbsp count=2]}

C) @FunctionalInterface
[nbsp count=3]public interface Useful{ default void doStuff(){} }

D) public interface Useful {
[nbsp count=6]abstract void doStuff();
[nbsp count=6]abstract void doCalc();
[nbsp count=2]}


5. Given the code fragment:

public class App {
[nbsp count=4]public static void main(String[] args) {
[nbsp count=8]String s = "Java";
[nbsp count=8]String n = "SE";
[nbsp count=8]// Line n1
[nbsp count=8]System.out.println(sf.apply(s, n));
[nbsp count=4]}

Which code fragment, when inserted at Line n1, prints JavaSE?

A) BiFunction<String, String, String> sf = (s1, n1) -> s1.concat(n1);
B) BiFunction<String, String> sf = (s1, n1) -> s1.concat(n1);
C) Function<String, String> sf = (s1, n1) -> s1.concat(n1);
D) Function<String, String, String> sf = (s1, n1) -> s1.concat(n1);


6. Given the code fragment:

Arrays.asList("Fred", "Jim", "Sheila")
[nbsp count=4].stream()
[nbsp count=4].peek(System.out::println)
[nbsp count=4].allMatch(s -> s.startsWith("F"));

What is the result?

A) Fred
[nbsp count=2]Jim
[nbsp count=2]Sheila

B) Fred
[nbsp count=2]Jim

C) Fred
D) Compilation fails.


7. Given the code fragment:

LocalDate date1 = LocalDate.of(2016, Month.JANUARY, 1);
LocalDateTime date2 = LocalDateTime.of(2017, Month.JUNE, 1, 1, 1);
Period p = Period.between(date1, date2);
System.out.print(p.getYears() + ":" + p.getMonths() + ":" + p.getDays());

What is the result?

A) 1:5:0
B) 1:6:0
C) 0:0:0
D) Compilation fails.


8. Given the code fragment:

class MyResource1 implements Closeable {
[nbsp count=4]public void close() {
[nbsp count=8]System.out.print("r1 ");
[nbsp count=4]}

class MyResource2 implements AutoCloseable {
[nbsp count=4]public void close() throws IOException {
[nbsp count=8]System.out.print("r2 ");
[nbsp count=8]throw new IOException();
[nbsp count=4]}

public class App2 {
[nbsp count=4]public static void main(String[] args) {
[nbsp count=8]try (MyResource1 r1 = new MyResource1();
[nbsp count=14]MyResource2 r2 = new MyResource2();) {
[nbsp count=12]System.out.print("try ");
[nbsp count=8]} catch (Exception e) {
[nbsp count=12]System.out.print("catch ");
[nbsp count=12]for (Throwable t : e.getSuppressed()) {
[nbsp count=16]System.out.println(t.getClass().getName());
[nbsp count=12]}
[nbsp count=8]}
[nbsp count=4]}

What is the result?

A) try r2 r1 catch
B) try r2 r1 catch
C) try r1 r2 catch
D) Compilation fails.



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


Source: Oracle, Exam 813: Upgrade to Java SE 8 OCP

About the author

Mushfiq Mammadov

Leave a Comment


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