Weekend Sale Limited Time 70% Discount Offer - Ends in 0d 00h 00m 00s - Coupon code: 70percent

Oracle 1z0-809 Java SE 8 Programmer II Exam Practice Test

Demo: 29 questions
Total 196 questions

Java SE 8 Programmer II Questions and Answers

Question 1

Given the code fragment:

String str = “Java is a programming language”;

ToIntFunction indexVal = str: : indexOf; //line n1

int x = indexVal.applyAsInt(“Java”);//line n2

System.out.println(x);

What is the result?

Options:

A.

0

B.

1

C.

A compilation error occurs at line n1.

D.

A compilation error occurs at line n2.

Question 2

Given:

class UserException extends Exception { }

class AgeOutOfLimitException extends UserException { }

and the code fragment:

class App {

public void doRegister(String name, int age)

throws UserException, AgeOutOfLimitException {

if (name.length () <= 60) {

throw new UserException ();

} else if (age > 60) {

throw new AgeOutOfLimitException ();

} else {

System.out.println(“User is registered.”);

}

}

public static void main(String[ ] args) throws UserException {

App t = new App ();

t.doRegister(“Mathew”, 60);

}

}

What is the result?

Options:

A.

User is registered.

B.

An AgeOutOfLimitException is thrown.

C.

A UserException is thrown.

D.

A compilation error occurs in the main method.

Question 3

Given:

and the command:

java Product 0

What is the result?

Options:

A.

An AssertionError is thrown.

B.

A compilation error occurs at line n1.

C.

New Price: 0.0

D.

A NumberFormatException is thrown at run time.

Question 4

Given:

final class Folder {//line n1

//line n2

public void open () {

System.out.print(“Open”);

}

}

public class Test {

public static void main (String [] args) throws Exception {

try (Folder f = new Folder()) {

f.open();

}

}

}

Which two modifications enable the code to print Open Close? (Choose two.)

Options:

A.

Replace line n1 with:class Folder implements AutoCloseable {

B.

Replace line n1 with:class Folder extends Closeable {

C.

Replace line n1 with:class Folder extends Exception {

D.

At line n2, insert:final void close () {System.out.print(“Close”);}

E.

At line n2, insert:public void close () throws IOException {System.out.print(“Close”);}

Question 5

Given the code fragment:

What is the result?

Options:

A.

5 : 3 : 6

B.

6 : 5 : 6

C.

3 : 3 : 4

D.

4 : 4 : 4

Question 6

Given:

class Bird {

public void fly () { System.out.print(“Can fly”); }

}

class Penguin extends Bird {

public void fly () { System.out.print(“Cannot fly”); }

}

and the code fragment:

class Birdie {

public static void main (String [ ] args) {

fly( ( ) -> new Bird ( ));

fly (Penguin : : new);

}

/* line n1 */

}

Which code fragment, when inserted at line n1, enables the Birdie class to compile?

Options:

A.

static void fly (Consumer bird) {bird :: fly ();}

B.

static void fly (Consumer bird) {bird.accept( ) fly ();}

C.

static void fly (Supplier bird) {bird.get( ) fly ();}

D.

static void fly (Supplier bird) {LOST

Question 7

Given the content:

and given the code fragment:

Which two code fragments, when inserted at line 1 independently, enable the code to print “Wie geht’s?”

Options:

A.

currentLocale = new Locale (“de”, “DE”);

B.

currentLocale = new Locale.Builder ().setLanguage (“de”).setRegion (“DE”).build();

C.

currentLocale = Locale.GERMAN;

D.

currentlocale = new Locale();currentLocale.setLanguage (“de”);currentLocale.setRegion (“DE”);

E.

currentLocale = Locale.getInstance(Locale.GERMAN,Locale.GERMANY);

Question 8

Given the definition of the Emp class:

public class Emp

private String eName;

private Integer eAge;

Emp(String eN, Integer eA) {

this.eName = eN;

this.eAge = eA;

}

public Integer getEAge () {return eAge;}

public String getEName () {return eName;}

}

and code fragment:

Listli = Arrays.asList(new Emp(“Sam”, 20), New Emp(“John”, 60), New Emp(“Jim”, 51));

Predicate agVal = s -> s.getEAge() > 50;//line n1

li = li.stream().filter(agVal).collect(Collectors.toList());

Stream names = li.stream()map.(Emp::getEName);//line n2

names.forEach(n -> System.out.print(n + “ “));

What is the result?

Options:

A.

Sam John Jim

B.

John Jim

C.

A compilation error occurs at line n1.

D.

A compilation error occurs at line n2.

Question 9

Given the Greetings.properties file, containing:

and given:

What is the result?

Options:

A.

Compilation fails.

B.

GOODBY_MSG

C.

Hello, everyone!

D.

Goodbye everyone!

E.

HELLO_MSG

Question 10

Which code fragment is required to load a JDBC 3.0 driver?

Options:

A.

Connection con = Connection.getDriver(“jdbc:xyzdata://localhost:3306/EmployeeDB”);

B.

Class.forName(“org.xyzdata.jdbc.NetworkDriver”);

C.

Connection con = DriverManager.getConnection(“jdbc:xyzdata://localhost:3306/EmployeeDB”);

D.

DriverManager.loadDriver (“org.xyzdata.jdbc.NetworkDriver”);

Question 11

Given:

Your design requires that:

  • fuelLevel of Engine must be greater than zero when the start() method is invoked.
  • The code must terminate if fuelLevel of Engine is less than or equal to zero.

Which code fragment should be added at line n1 to express this invariant condition?

Options:

A.

assert (fuelLevel) : “Terminating…”;

B.

assert (fuelLevel > 0) : System.out.println (“Impossible fuel”);

C.

assert fuelLevel < 0: System.exit(0);

D.

assert fuelLevel > 0: “Impossible fuel” ;

Question 12

Given:

Which option fails?

Options:

A.

Foo mark = new Foo (“Steve”, 100);

B.

Foo pair = Foo.twice (“Hello World!”);

C.

Foo percentage = new Foo(“Steve”, 100);

D.

Foo grade = new Foo <> (“John”, “A”);

Question 13

Given:

public enum USCurrency {

PENNY (1),

NICKLE(5),

DIME (10),

QUARTER(25);

private int value;

public USCurrency(int value) {

this.value = value;

}

public int getValue() {return value;}

}

public class Coin {

public static void main (String[] args) {

USCurrency usCoin =new USCurrency.DIME;

System.out.println(usCoin.getValue()):

}

}

Which two modifications enable the given code to compile? (Choose two.)

Options:

A.

Nest the USCurrency enumeration declaration within the Coin class.

B.

Make the USCurrency enumeration constructor private.

C.

Remove the new keyword from the instantion of usCoin.

D.

Make the getter method of value as a static method.

E.

Add the final keyword in the declaration of value.

Question 14

Given:

class Student {

String course, name, city;

public Student (String name, String course, String city) {

this.course = course; this.name = name; this.city = city;

}

public String toString() {

return course + “:” + name + “:” + city;

}

public String getCourse() {return course;}

public String getName() {return name;}

public String getCity() {return city;}

and the code fragment:

List stds = Arrays.asList(

new Student (“Jessy”, “Java ME”, “Chicago”),

new Student (“Helen”, “Java EE”, “Houston”),

new Student (“Mark”, “Java ME”, “Chicago”));

stds.stream()

.collect(Collectors.groupingBy(Student::getCourse))

.forEach(src, res) -> System.out.println(scr));

What is the result?

Options:

A.

A compilation error occurs.

B.

Java EEJava ME

C.

[Java EE: Helen:Houston][Java ME: Jessy:Chicago, Java ME: Mark:Chicago]

D.

[Java ME: Jessy:Chicago, Java ME: Mark:Chicago][Java EE: Helen:Houston]

Question 15

Given:

and the code fragment:

What is the result?

Options:

A.

A compilation error occurs at line n2.

B.

A compilation error occurs because the try block doesn’t have a catch or finally block.

C.

A compilation error occurs at line n1.

D.

The program compiles successfully.

Question 16

Given:

Book.java:

public class Book {

private String read(String bname) { return “Read” + bname }

}

EBook.java:

public class EBook extends Book {

public class String read (String url) { return “View” + url }

}

Test.java:

public class Test {

public static void main (String[] args) {

Book b1 = new Book();

b1.read(“Java Programing”);

Book b2 = new EBook();

b2.read(“http://ebook.com/ebook”);

}

}

What is the result?

Options:

A.

Read Java ProgrammingView http:/ ebook.com/ebook

B.

Read Java ProgrammingRead http:/ ebook.com/ebook

C.

The EBook.java file fails to compile.

D.

The Test.java file fails to compile.

Question 17

Given the code fragment:

What is the result?

Options:

A.

4000 : 2000

B.

4000 : 1000

C.

1000 : 4000

D.

1000 : 2000

Question 18

Given:

class RateOfInterest {

public static void main (String[] args) {

int rateOfInterest = 0;

String accountType = “LOAN”;

switch (accountType) {

case “RD”;

rateOfInterest = 5;

break;

case “FD”;

rateOfInterest = 10;

break;

default:

assert false: “No interest for this account”; //line n1

}

System.out.println (“Rate of interest:” + rateOfInterest);

}

}

and the command:

java –ea RateOfInterest

What is the result?

Options:

A.

Rate of interest: 0

B.

An AssertionError is thrown.

C.

No interest for this account

D.

A compilation error occurs at line n1.

Question 19

Given:

class UserException extends Exception { }

class AgeOutOfLimitException extends UserException { }

and the code fragment:

class App {

public void doRegister(String name, int age)

throws UserException, AgeOutOfLimitException {

if (name.length () < 6) {

throw new UserException ();

} else if (age >= 60) {

throw new AgeOutOfLimitException ();

} else {

System.out.println(“User is registered.”);

}

}

public static void main(String[ ] args) throws UserException {

App t = new App ();

t.doRegister(“Mathew”, 60);

}

}

What is the result?

Options:

A.

User is registered.

B.

An AgeOutOfLimitException is thrown.

C.

A UserException is thrown.

D.

A compilation error occurs in the main method.

Question 20

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?

Options:

A.

The program prints true.

B.

The program prints false.

C.

A compilation error occurs. To ensure successful compilation, replace line n1 with:boolean equals (Book obj) {

D.

A compilation error occurs. To ensure successful compilation, replace line n2 with:System.out.println (b1.equals((Object) b2));

Question 21

Given the code fragment:

9. Connection conn = DriveManager.getConnection(dbURL, userName, passWord);

10. String query = “SELECT id FROM Employee”;

11. try (Statement stmt = conn.createStatement()) {

12. ResultSet rs = stmt.executeQuery(query);

13.stmt.executeQuery(“SELECT id FROM Customer”);

14. while (rs.next()) {

15. //process the results

16.System.out.println(“Employee ID: “+ rs.getInt(“id”));

17.}

18. } catch (Exception e) {

19. System.out.println (“Error”);

20. }

Assume that:

The required database driver is configured in the classpath.

The appropriate database is accessible with the dbURL, userName, and passWord exists.

The Employee and Customer tables are available and each table has id column with a few records and the SQL queries are valid.

What is the result of compiling and executing this code fragment?

Options:

A.

The program prints employee IDs.

B.

The program prints customer IDs.

C.

The program prints Error.

D.

compilation fails on line 13.

Question 22

Given the records from the STUDENT table:

Given the code fragment:

Assume that the URL, username, and password are valid.

What is the result?

Options:

A.

The STUDENT table is not updated and the program prints:114 : John : john@uni.com

B.

The STUDENT table is updated with the record:113 : Jannet : jannet@uni.comand the program prints:114 : John : john@uni.com

C.

The STUDENT table is updated with the record:113 : Jannet : jannet@uni.comand the program prints:113 : Jannet : jannet@uni.com

D.

A SQLException is thrown at run time.

Question 23

Given that course.txt is accessible and contains:

Course : : Java

and given the code fragment:

public static void main (String[ ] args) {

int i;

char c;

try (FileInputStream fis = new FileInputStream (“course.txt”);

InputStreamReader isr = new InputStreamReader(fis);) {

while (!isr.close()) { //line n1

isr.skip(2);

i = isr.read ();

c = (char) i;

System.out.print(c);

}

} catch (Exception e) {

e.printStackTrace();

}

}

What is the result?

Options:

A.

ur :: va

B.

ueJa

C.

The program prints nothing.

D.

A compilation error occurs at line n1.

Question 24

Given the definition of the Book class:

Which statement is true about the Book class?

Options:

A.

It demonstrates encapsulation.

B.

It is defined using the factory design pattern.

C.

It is defined using the singleton design pattern.

D.

It demonstrates polymorphism.

E.

It is an immutable class.

Question 25

Given the code fragment:

Path path1 = Paths.get(“/app/./sys/”);

Path res1 = path1.resolve(“log”);

Path path2 = Paths.get(“/server/exe/”);

Path res1 = path1.resolve(“/readme/”);

System.out.println(res1);

System.out.println(res2);

What is the result?

Options:

A.

/app/sys/log/readme/server/exe

B.

/app/log/sys/server/exe/readme

C.

/app/./sys/log/readme

D.

/app/./sys/log/server/exe/readme

Question 26

Given:

public class Test {

private T t;

public T get () {

return t;

}

public void set (T t) {

this.t = t;

}

public static void main (String args [ ] ) {

Test type = new Test<>();

Test type 1 = new Test ();//line n1

type.set(“Java”);

type1.set(100);//line n2

System.out.print(type.get() + “ “ + type1.get());

}

}

What is the result?

Options:

A.

Java 100

B.

java.lang.string@java.lang.Integer@

C.

A compilation error occurs. To rectify it, replace line n1 with:Test type1 = new Test<>();

D.

A compilation error occurs. To rectify it, replace line n2 with:type1.set (Integer(100));

Question 27

Given:

and the code fragment:

Which modification enables the code fragment to print Speaker?

Options:

A.

Implement Predicate in the Product.ProductFilter class and replace line n2 with .filter (p -> p.ProductFilter.test (p))

B.

Replace line n1 with:public static boolean isAvailable (Product p) {

C.

Replace line n2 with:.filter (p -> p.ProductFilter: :isAvailable (p))

D.

Replace line n2 with:.filter (p -> Product: :ProductFilter: :isAvailable ())

Question 28

Given the code fragment:

public class FileThread implements Runnable {

String fName;

public FileThread(String fName) { this.fName = fName; }

public void run () System.out.println(fName);}

public static void main (String[] args) throws IOException, InterruptedException {

ExecutorService executor = Executors.newCachedThreadPool();

Stream listOfFiles = Files.walk(Paths.get(“Java Projects”));

listOfFiles.forEach(line -> {

executor.execute(new FileThread(line.getFileName().toString())); //

line n1

});

executor.shutdown();

executor.awaitTermination(5, TimeUnit.DAYS);//

line n2

}

}

The Java Projects directory exists and contains a list of files.

What is the result?

Options:

A.

The program throws a runtime exception at line n2.

B.

The program prints files names concurrently.

C.

The program prints files names sequentially.

D.

A compilation error occurs at line n1.

Question 29

Given the code fragment:

Assume that the value of now is 6:30 in the morning.

What is the result?

Options:

A.

An exception is thrown at run time.

B.

0

C.

60

D.

1

Demo: 29 questions
Total 196 questions