1z0-809 Java SE 8 Programmer II (OCP) Practice Questions
Prepare for 1z0-809 with more than an answer.
- Level
- Professional
- Valid for
- Certification does not expire, but Oracle recommends staying current with newer Java versions
Domains covered on the exam 12
- Java Class Design
- Advanced Java Class Design
- Generics and Collections
- Lambda Built-in Functional Interfaces
- Java Stream API
- Exceptions and Assertions
- Use Java SE 8 Date/Time API
- Java I/O Fundamentals
- Java File I/O (NIO.2)
- Java Concurrency
- Building Database Applications with JDBC
- Localization
- 1
Given the code fragments:
void doStuff() throws ArithmeticException, NumberFormatException, Exception {
if (Math.random() )-1 throw new Exception (“Try again');
}
and
try {
doStuff ( ):
} catch (ArithmeticException | NumberFormatException | Exception e) {
System.out.println (e.getMessage()); }
catch (Exception e) {
System.out.println (e.getMessage()); }
}
Which modification enables the code to print Try again?
Show answer details
Correct answer: C
C
- 2
Given the definition of the Country class:
public class country {
public enum Continent {ASIA, EUROPE}
String name;
Continent region;
public Country (String na, Continent reg) {
name = na, region = reg;
}
public String getName () {return name;}
public Continent getRegion () {return region;}
}
and the code fragment:
List(Country) couList = Arrays.asList (
new Country (“Japan', Country.
Continent.
ASIA),
new Country (“Italy', Country.
Continent.
EUROPE),
new Country (“Germany', Country.
Continent.
EUROPE));
Map(Country.
Continent, List(String)) regionNames = couList.stream ()
.collect(Collectors.groupingBy (Country ::getRegion,
Collectors.mapping(Country::getName, Collectors.toList()))));
System.out.println(regionNames);
What is the output?
Show answer details
Correct answer: B
B
- 3
Given the code fragment:
Map(Integer, String) books = new TreeMap()();
books.put (1007, “A');
books.put (1002, “C');
books.put (1001, “B');
books.put (1003, “B');
System.out.println (books);
What is the result?
Show answer details
Correct answer: B
Reference:
TreeMap inherits SortedMap and automatically sorts the element's key
- 4
Given:
class Book {
int id;
String name;
public Book (int id, String name) {
this.id = id;
this.name = name;
}
public boolean equals (Object obj) { //line n1
boolean output = false;
Book b = (Book) obj;
if (this.name.equals(b name))}
output = true;
}
return output;
}
}
and the code fragment:
Book b1 = new Book (101, “Java Programing');
Book b2 = new Book (102, “Java Programing');
System.out.println (b1.equals(b2)); //line n2
Which statement is true?
Show answer details
Correct answer: A
A
- 5
Given the content of /resourses/Message.properties:
welcome1='Good day!'
and given the code fragment:
Properties prop = new Properties ();
FileInputStream fis = new FileInputStream (“/resources/Message.properties');
prop.load(fis);
System.out.println(prop.getProperty(“welcome1'));
System.out.println(prop.getProperty(“welcome2', “Test'));//line n1
System.out.println(prop.getProperty(“welcome3'));
What is the result?
Show answer details
Correct answer: C
C
- 6
Which action can be used to load a database driver by using JDBC3.0?
Show answer details
Correct answer: C
C
- 7
A systems administrator needs to write a script to find all symbolic links within the
/usr/bindirectory tree. Which NIO.2 code fragment correctly achieves this?flowchart TD Start([Start]) --> Walk{Files.walk("/usr/bin")} Walk --> Filter{Filter for Symbolic Links} Filter --> Print[Print Path] Print --> End([End])Show answer details
Correct answer: C
Files.walk(path)returns aStreamthat recursively walks the directory tree. The requirement is to check each entry to see if it's a symbolic link. TheFiles::isSymbolicLinkmethod reference is a perfectPredicatefor this filtering task.Files.listonly lists the immediate contents of a directory, not the whole tree. - 8
Given the definition of the Vehicle class:
Class Vehicle {
int distance;
Vehicle (int x) {
this distance = x;
}
public void increSpeed(int time) {
int timeTravel = time; //line n1
//line n3
class Car {
int value = 0;
public void speed () {
value = distance /timeTravel;//line n2
System.out.println (“Velocity with new speed'+value+'kmph');
}
}
speed();//line n3
}
}
and this code fragment:
Vehicle v = new Vehicle (100);
v.increSpeed(60);
What is the result?
Show answer details
Correct answer: A
A
- 9
Given:
IntStream stream = IntStream.of (1,2,3);
IntFunction(Integer) inFu= x -) y -) x*y;//line n1
IntStream newStream = stream.map(inFu.apply(10));//line n2 newStream.forEach(System.out::print);
Which modification enables the code fragment to compile?
Show answer details
Correct answer: B
B
- 10
Given the code fragment:
List(Integer) values = Arrays.asList (1, 2, 3);
values.stream ()
.map(n -) n*2)//line n1
.peek(System.out::print)//line n2
.count();
What is the result?
Show answer details
Correct answer: A
A
