Java OCA OCP Practice Question 3184

Question

Consider the following program:

import java.nio.file.*;

public class Main {
     public static void main(String []args) {
             Path javaPath = Paths.get("D:\\OCPJP7\\programs\\NIO2\\src\\Main.java").normalize();
             Path classPath =Paths.get("D:\\OCPJP7\\programs\\NIO2\\src\\Main.class").normalize();
             Path result = javaPath.relativize(classPath);
             if(result == null) {
                     System.out.println("relativize failed");
             } else if(result.equals(Paths.get(""))) {
                     System.out.println("relative paths are same, so relativize returned empty path");
             } else {
                     System.out.println(result);
             }// w  w w .j ava  2s .c  o m
     }
}

Which of the following options correctly shows the output of this program?

  • A) The program prints the following: relativize failed.
  • B) The program prints the following: relative paths are same, so relativize returned empty path.
  • C) The program prints the following: ..\Main.class.
  • D) The program prints the following: ..\Main.java.


C)

Note

The relativize() method constructs a relative path between this path and a given path.

In this case, the paths for both the files are the same and they differ only in the file names (Main.java and Main.class).

The relative comparison of paths is performed from the given path to the passed path to the relativize method, so it prints ..\Main.class.

The normalize() method removes any redundant name elements in a path.

In this program, there are no redundant name elements, so it has no impact on the output of this program.




PreviousNext

Related