Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium
Question 55
Question ID: UKOCP38399
Given code:
- package com.udayankhattry.ocp;
- import java.nio.file.Paths;
- public class Test {
- public static void main(String[] args) {
- var file1 = Paths.get("F:\\A\\B\\C");
- var file2 = Paths.get("Book.java");
- System.out.println(file1.resolve(file2));
- System.out.println(file1.resolveSibling(file2));
- }
- }
What is the result?
-
A
Book.java
Book.java
-
B
F:\A\B\Book.java
F:\A\B\C\Book.java
-
C
F:\A\B\C\Book.java
F:\A\B\Book.java
-
D
F:\A\B\C\Book.java
Book.java
-
E
Book.java
F:\A\B\Book.java
Reveal correct answer
Correct answer: C
Explanation
UKOCP38399:
Variables 'file1' and 'file2' infer to Path type.
Method Paths.get(...) converts a path String, or a sequence of strings that when joined form a path string, to a Path.
file1.resolve(file2) resolves file2 against file1. file1 is an absolute path and file2 is a relative path, hence resolve method returns Path object referring to 'F:\A\B\C\Book.java'.
file1.resolveSibling(file2) resolves file2 against parent path of file1. Parent path of file1 is: 'F:\A\B\', hence resolveSibling method returns Path object referring to 'F:\A\B\Book.java'.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
