nerdexam
Oracle

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…

Java I/O API

Question

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 + " " + p3); What is the result of compiling and running this code?

Options

  • ACompiler error
  • Bmango/guava mango/apple
  • Cmango/java apple
  • Dmango/guava
  • ENone of these

How the community answered

(37 responses)
  • A
    3% (1)
  • B
    3% (1)
  • C
    5% (2)
  • D
    76% (28)
  • E
    14% (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) - assumes resolve("../") normalizes the path, but Path doesn'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 only p1 printed, this could be considered, but it still doesn't compile, and the print statement includes both p1 and p3 separated 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

#Path.resolve()#NIO Path API#relative path resolution#path semantics

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice