Friday, August 6, 2010

Files: Directories.java

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)

Chapter 6, Question 7

 
 
 
 
 
 
 
 
 
 
 
 
 
If you invoke the following command at runtime
 
java Directories file2.txt
 
is issued from a directory that has two subdirectories, “dir1” and “dir2”, and that “dir1” has a file “file1.txt” and “dir2” has a file “file2.txt”, and the output is “false true”, which set(s) of code fragments must be inserted? (Choose all that apply.)
 
A.

String path = d;
System.out.print(file.exists() + " ");

B.

String path = d;
System.out.print(file.isFile() + " ");

C.

String path = File.separator + d;
System.out.print(file.exists() + " ");

D.

String path = File.separator + d;
System.out.print(file.isFile() + " ");

 
Options A and B are correct. Because you are invoking the program from the directory whose direct subdirectories are to be searched, you don’t start your path with a File.separator character. The exists() method tests for either files or directories; the isFile() method tests only for files. Since we’re looking for a file, both methods work.

No comments:

Post a Comment