Found a confusing question while learning about file separators
Suppose that the file c:\book\java exists. Which of the following lines of code creates an object that represents the file? (Choose all that apply.)
1. new File("c:\book\java");
2. new File("c:\\book\\java");
3. new File("c:/book/java");
4. new File("c://book//java");
5. None of the above
the book (assumig a dos based file system) says that
- is correct because Java requires a backslash to be escaped with another backslash.
- is also correct because Java will convert the slashes to the right one when working with paths..
Can 4 also be correct by this logic?
Given that the question is
Suppose that the file
c:\book\javaexists. Which of the following lines of code creates an object that represents the file? (Choose all that apply.)
- new File("c:\book\java");
- new File("c:\book\java");
- new File("c:/book/java");
- new File("c://book//java");
- None of the above
2 and 3 are obviously correct. So, does the File object in 4 "create an ojbect that represents the file"?
Yes it does.
Assuming C:\book\java exists, this code
public static void main( String[] args ) throws IOException
{
File f = new File( args[ 0 ] );
System.err.printf( "args[0]: %s\n", args[ 0 ] );
System.err.printf( "Path: %s\n", f.getCanonicalPath() );
}
produces this output:
args[0]: C://book//java
Path: C:\book\java
So new File( "C://book//java" ) most definitely "creates an object that represents the file" and is also a correct answer.
Any argument that it does not is literally incorrect. The question is whether or not the string "creates an object that represents the file". C://book//java demonstrably does exactly that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With