Question ID :1002740579101 What will the following are correct statments regarding this program, if it is run from directory: c:\work ? Assume that file Abc.txt does not exist.
Options - (Choose two) a) f.getPath will return c:\work\Abc.txt b) f.getCanonicalPath will return c:\work\Abc.txt c) f.getPath will return ..\work\Abc.txt d) f.getAbsolutePath will return c:\work\Abc.txt e) f.getAbsolutePath will return c:\work\..\work\Abc.txt Answer and Explanation - (b) and (e) Creating a File object does not reqire the file to be present. It also does not create any file. getPath : returns whatever was passed to the constructor of File class while creating a file object. So: Abc.txt getAbsolutePath : returns the root path + whatever was passed to the constructor. So: c:\work\Abc.txt getCanonicalPath : returns the root path + whatever was passed to the constructor - Any redundent path. For eg. for new File("../work/Abc.txt"), AbsolutePath is: c:\work\..\work\Abc.txt but CanonicalPath is c:\work\Abc.txt . Note that, it resolved the work/../work to simply work. My Doubts - 1) Option c is is also right, I tested this and found true. (Try outputting these values from dir c:\work) 2) Explanation is incorrect because what is passed to the constuctor is - "../work/Abc.txt", also if (e) is right (c) has to be right, because (e) implies (c) as per the API. Try running this code from c:\work directory -
I think your guesses are correct. If you can back up your statements with the code executions, then you are definitely right I've notified the JQ+ team. [ March 20, 2002: Message edited by: Valentin Crettaz ]