1. Given the code fragment:
public class App {
[nbsp count=4]void calcBill() {
[nbsp count=8]// Line n1
[nbsp count=8]new Invoice().print();
[nbsp count=4]}
}
Which code fragment can be inserted at Line n1 to enable the class compile?
A) private class Invoice {
[nbsp count=6]void print() { System.out.println("Invoice Printed"); }
[nbsp count=2]}
B) public class Invoice {
[nbsp count=6]void print() { System.out.println("Invoice Printed"); }
[nbsp count=2]}
C) class Invoice {
[nbsp count=6]void print() { System.out.println("Invoice Printed"); }
[nbsp count=2]}
D) protected class Invoice {
[nbsp count=6]void print() { System.out.println("Invoice Printed"); }
[nbsp count=2]}
2. Given:
public interface MyInt {
[nbsp count=4]public void method1() {
[nbsp count=8]System.out.println("method1");
[nbsp count=4]}
[nbsp count=4]public default void method2() {
[nbsp count=8]System.out.println("method2");
[nbsp count=4]}
[nbsp count=4]public static void method3() {
[nbsp count=8]System.out.println("method3");
[nbsp count=4]}
[nbsp count=4]public abstract void method4();
}
Which statement is true?
A) Only method4()
compiles
B) Only method2()
and method4()
compiles.
C) Only method2()
, method3()
, and method4()
compiles.
D) MyInt.java
compiles.
3. Given the code fragment:
public static void main(String[] args) {
[nbsp count=4]List<String> courses = Arrays.asList("Java", "Oracle", "JSF", "EJB");
[nbsp count=4]// Line n1
[nbsp count=4]System.out.println(count);
}
Which code fragment can be inserted at Line n1 to enable the code to print 2?
A) int count = courses.stream().filter(s -> s.startsWith("J")).count();
B) long count = courses.stream().filter(s -> s.startsWith("J")).count();
C) int count = courses.filter(s -> s.startsWith("J")).stream().count();
D) long count = courses.filter(s -> s.startsWith("J")).stream().count();
4. Given the code fragment:
public class App {
[nbsp count=4]public static void main(String[] args) {
[nbsp count=8]String[] fruits = {"banana", "apple", "pears", "grapes"};
[nbsp count=8]Arrays.sort(fruits, (a, b) -> a.compareTo(b));
[nbsp count=8]for (String s : fruits) {
[nbsp count=12]System.out.print(" " + s);
[nbsp count=8]}
[nbsp count=4]}
}
What is the result?
A) apple banana grapes pears
B) pears grapes banana apple
C) banana apple pears grapes
D) Compilation fails.
5. 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.
6. Given that /report/jun.txt
and report/data/jundata.txt
files are accessible and given the code fragment:
public static void main(String[] args) {
[nbsp count=4]try (Stream<Path> st1 = Files.find(Paths.get("/report"), 2, (p, a) ->
[nbsp count=8]p.toString().endsWith("txt"));
[nbsp count=8]Stream<Path> st2 = Files.walk(Paths.get("/report"), 2); ) {
[nbsp count=12]st1.forEach(s -> System.out.println("Found: " + s));
[nbsp count=12]st2.filter(s -> s.toString()
[nbsp count=16].endsWith("txt"))
[nbsp count=16].forEach(s -> System.out.println("Walked: " + s));
[nbsp count=8]} catch (IOException ioe) {
[nbsp count=12]System.out.println("Exception");
[nbsp count=8]}
}
What is the result?
A) Found: \result\data\jundata.txt
[nbsp count=3]Found: \result\jun.txt
[nbsp count=3]Walked: \result\data\jundata.txt
[nbsp count=3]Walked: \result\jun.txt
B) Found: \result\data\jundata.txt
[nbsp count=3]Found: \result\jun.txt
[nbsp count=3]Walked: \result\data\jundata.txt
[nbsp count=3]Walked: \result
[nbsp count=3]Walked: \result\jun.txt
[nbsp count=3]Walked: \result\data\
C) Found: \result\jun.txt
[nbsp count=3]Walked: \result\data\jundata.txt
[nbsp count=3]Walked: \result\jun.txt
D) Found: \result\jun.txt
[nbsp count=3]Walked: \result
[nbsp count=3]Walked: \result\jun.txt
[nbsp count=3]Walked: \result\data\
[nbsp count=3]Walked: \result\data\jundata.txt
7. Given the code fragment:
public static void main(String[] args) {
[nbsp count=4]Stream<Integer> nums = Stream.of(1, 2, 3, 4, 5);
[nbsp count=4]nums.filter(n -> n % 2 == 1);
[nbsp count=4]nums.forEach(p -> System.out.print(p));
}
What is the result?
A) 135
B) 12345
C) Compilation fails.
D) An exception is thrown at runtime.
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=15]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 java.io.IOException
B) try r2 r1 catch
C) try r1 r2 catch
D) Compilation fails.
Answers:
1. C
2. C
3. B
4. A
5. D
6. A
7. D
8. B
Source: Oracle, Exam 810: Upgrade Java SE 7 to Java SE 8 OCP