nerdexam
Oracle

1Z0-819 · Question #80

Given: String originalPath = "data\\projects\\a-project\\..\\..\\another-project"; Path path = Paths.get(originalPath); System.out.print(path.normalize()); What is the result?

The correct answer is A. data\another-project. Path.normalize() resolves . (current dir) and .. (parent dir) segments, so data\\projects\\a-project\\..\\.. collapses by stepping up twice from a-project - first to projects, then to data - leaving data\\another-project, which is A. Why the distractors fail: B ignores the…

Java I/O API

Question

Given: String originalPath = "data\projects\a-project\..\..\another-project"; Path path = Paths.get(originalPath); System.out.print(path.normalize()); What is the result?

Options

  • Adata\another-project
  • Bdata\projects\a-project\another-project
  • Cdata\projects\a-project....\another-project
  • Ddata\projects\a-project..\another-project

How the community answered

(58 responses)
  • A
    81% (47)
  • B
    3% (2)
  • C
    10% (6)
  • D
    5% (3)

Explanation

Path.normalize() resolves . (current dir) and .. (parent dir) segments, so data\\projects\\a-project\\..\\.. collapses by stepping up twice from a-project - first to projects, then to data - leaving data\\another-project, which is A.

Why the distractors fail:

  • B ignores the .. segments entirely, as if normalize() were never called.
  • C is the raw, un-normalized path - this would be the output of path.toString(), not path.normalize().
  • D only removes one .. instead of both, which is a partial (incorrect) normalization.

Memory tip: Think of .. as a "delete the previous folder" instruction - count your .. segments and cancel them out one by one from right to left against the real path segments. Two .. after a-project erase a-project then projects, leaving only data.

Topics

#Path.normalize()#Path manipulation#Parent directory references#NIO.file API

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice