nerdexam
Oracle

1Z0-809 · Question #67

Given the code fragment: ``java Path path1 = Paths.get("/app/../sys/"); Path res1 = path1.resolve("log"); Path path2 = Paths.get("/server/exe/"); Path res2 = path2.resolve("../readme/")…

The correct answer is C. /app/sys/log /server/readme. Option C is correct because Path.resolve() appends a relative path to the base path and semantically processes parent-directory (..) references during path concatenation. For res1, path1 holds /app/../sys/ - the .. cancels the app segment, so the effective path is /sys/, and…

Question

Given the code fragment:
Path path1 = Paths.get("/app/../sys/");
Path res1 = path1.resolve("log");
Path path2 = Paths.get("/server/exe/");
Path res2 = path2.resolve("../readme/");
System.out.println(res1);
System.out.println(res2);
What is the result?

Options

  • A/app/sys/log /server/exe/readme
  • B/app/log/sys /server/exe/readme
  • C/app/sys/log /server/readme
  • D/app/../sys/log /server/exe/readme

How the community answered

(29 responses)
  • A
    14% (4)
  • B
    3% (1)
  • C
    76% (22)
  • D
    7% (2)

Explanation

Option C is correct because Path.resolve() appends a relative path to the base path and semantically processes parent-directory (..) references during path concatenation. For res1, path1 holds /app/../sys/ - the .. cancels the app segment, so the effective path is /sys/, and appending "log" yields /app/sys/log. For res2, resolving "../readme/" against /server/exe/ applies the .. to step up from exe back to server, producing /server/readme.

Options A and B are distractors that swap or misplace path segments with no basis in how resolve() works. Option D is the most tempting wrong answer - it shows the raw, unnormalized string /app/../sys/log as if resolve() were pure string concatenation with no path logic; the second path /server/exe/readme also incorrectly drops the .. step-up entirely, leaving exe in the result. Option D is the classic trap for candidates who confuse resolve() with simple string appending.

Memory tip: Think of resolve() as "navigate from here" - a .. in the argument walks up the directory tree from the base, while a plain name walks down into a subdirectory. If the argument were an absolute path, resolve() would discard the base entirely and return only the argument.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice