1Z0-819 · Question #139
Given: Path p = Path.of("mango"); // line 1 Path p1 = p.resolve("guava"); // line 2 Path p2 = p.resolve("guava"); // line 3 Path p3 = p2.resolve("../", "apple"); // line 4 System.out.println(p1 + "…
The correct answer is D. mango/guava. Option D as the stated correct answer appears to be incorrect - the actual answer is A (Compiler error). Here's why: Line 4 is the problem. Path.resolve() only accepts a single argument - either a String or a Path. Calling p2.resolve("../", "apple") passes two String arguments…
Question
Options
- ACompiler error
- Bmango/guava mango/apple
- Cmango/java apple
- Dmango/guava
- ENone of these
How the community answered
(37 responses)- A3% (1)
- B3% (1)
- C5% (2)
- D76% (28)
- E14% (5)
Explanation
Option D as the stated correct answer appears to be incorrect - the actual answer is A (Compiler error).
Here's why:
Line 4 is the problem. Path.resolve() only accepts a single argument - either a String or a Path. Calling p2.resolve("../", "apple") passes two String arguments, for which no overload exists in java.nio.file.Path. This causes a compilation error before the code ever runs.
Why the distractors are wrong:
- B (
mango/guava mango/apple) - assumesresolve("../")normalizes the path, butPathdoesn't auto-normalize, and the code doesn't compile anyway. - C (
mango/java apple) - fabricated output with no basis in the API behavior. - D (
mango/guava) - if the code somehow ran and onlyp1printed, this could be considered, but it still doesn't compile, and the print statement includes bothp1andp3separated by a space. - E - is technically correct in spirit since the code doesn't compile, but A explicitly covers that.
Memory tip: Whenever you see resolve() with multiple arguments, immediately check the signature. Path.of() accepts varargs (String first, String... more), but Path.resolve() does not - that asymmetry is a classic exam trap. The only two overloads for resolve are resolve(String) and resolve(Path).
Note for the exam taker: If this question appeared on an actual exam with D marked correct, the answer key contains an error. Flag it - the code does not compile.
Topics
Community Discussion
No community discussion yet for this question.