Java SE 11 Programmer II

Here you have the best Oracle 1z0-816 practice exam questions

  • You have 40 total questions to study from
  • Each page has 5 questions, making a total of 8 pages
  • You can navigate through the pages using the buttons at the bottom
  • This questions were last updated on November 29, 2024
Question 1 of 40

Given the code fragment:

Path currentFile = Paths.get(“/scratch/exam/temp.txt”);

Path outputFile = Paths get(“/scratch/exam/new.txt”);

Path directory = Paths.get(“/scratch/”);

Files.copy(currentFile, outputFile);

Files.copy(outputFile, directory);

Files.delete (outputFile);

The /scratch/exam/temp.txt file exists. The /scratch/exam/new.txt and /scratch/new.txt files do not exist.

What is the result?

    Correct Answer: B

    The code attempts to copy the existing file /scratch/exam/temp.txt to /scratch/exam/new.txt without checking if the destination file already exists. Since the destination file does not exist initially, this operation succeeds. However, during the next copy operation, it tries to copy /scratch/exam/new.txt to the directory /scratch/, aiming to create a file /scratch/new.txt, which also does not exist. As both these operations would be successful, the program does not throw NoSuchFileException. Ultimately, it tries to delete /scratch/exam/new.txt, which was previously copied successfully. Therefore, the program does not throw NoSuchFileException but rather completes successfully. Therefore, the correct answer is that a FileAlreadyExistsException is thrown because /scratch/exam/new.txt is attempted to be copied again without specifying the option to replace existing files.

Question 2 of 40

Which two are functional interfaces? (Choose two.)

    Correct Answer: A, C

    A functional interface is an interface that has exactly one abstract method. Option A is a functional interface because it has only one abstract method, 'public void run()'. Option C is also a functional interface because it has one default method and one abstract method, 'public void run(String s)', with only the abstract method counting towards the functional interface requirement. Option B is not a functional interface as it contains two abstract methods. Option D is not a functional interface because it does not have any methods. Option E is incorrect since the @FunctionalInterface annotation is used on interfaces, not methods, leading to a compilation error.

Question 3 of 40

Given the declaration:

Examine this code fragment:

/* Loc1 */ class ProcessOrders { ... }

Which two annotations may be applied at Loc1 in the code fragment? (Choose two.)

    Correct Answer: C, D

    The @Resource annotation has a required element 'name', which must be provided in the annotation. Therefore, any valid usage of @Resource must include a value for 'name'. Option C includes both the required 'name' and the optional 'priority'. Option D only includes the required 'name', making it valid as well. Options A, B, and E are incorrect because they do not provide a value for the required 'name' element, leading to a compilation error.

Question 4 of 40

Given:

Which two interfaces can be used in lambda expressions? (Choose two.)

    Correct Answer: A, E

    To be eligible for use in lambda expressions, an interface must be a functional interface, which means it should have exactly one abstract method. MyInterface1 has one abstract method 'int method() throws Exception' and, as the private method pMethod() is not considered in the functional interface, it qualifies. MyInterface4 also qualifies because it has one abstract method 'void method()'. The other interfaces either have multiple abstract methods or do not meet the criteria for being a functional interface.

Question 5 of 40

Given this enum declaration:

Examine this code:

System.out.println(Alphabet.getFirstLetter());

What code should be written at line 3 to make this code print A?

    Correct Answer: C

    The method getFirstLetter should be static to be called without an instance of the enum. Also, using A.toString() will return the string representation of the constant A, which is what should be printed. Thus, the correct code at line 3 is: static String getFirstLetter() { return A.toString(); }.