Files, Directories, and Paths in Java - Quiz Explanation

The correct answers are indicated below, along with the text that explains the correct answers.

 
1. Which one of the following filenames is least likely to cause cross-platform compatibility problems?
Please select the best answer.
  A. list_of_compatibility_issues_for_software_projects.doc
  B. list:compatibility_issues
  C. compatibility_issues
  D. compatibility issues list
  The correct answer is C.To ensure the highest degree of cross-platform compatibility for a filename, you should restrict your filenames to ASCII characters plus periods and underscores.
In addition, you should keep your filenames to 32 characters or less. Answer A has more than 32 characters and would cause problems on Macintosh computers. Answer B would also be problematic on Macs because the colon is used as a file separator on Macintoshes and cannot be part of a filename. Answer D might be all right, but because Unix makes heavy use of a command line, a filename containing spaces is often inconvenient.


2. The class name FileViewer2$EncodingListener.class will cause problems if you try to use it on a Macintosh. Why?
Please select the best answer.
  A. Dollar signs are not permitted in Macintosh filenames
  B. Macintosh filenames cannot have extensions
  C. The class name exceeds the maximum number of characters allowed for Macintosh filenames
  D. The .class extension is reserved for Macintosh system use
  The correct answer is C. Mac filenames cannot be longer than 32 characters (including spaces). Dollar signs and extensions are no problems for Mac filenames and .class is not reserved for system use.

3. If you ran a Java application from the MS-DOS command prompt in which the current directory was C:\java\demo , then a relative path of ..\src\HelloWorld.java would refer to an absolute path of
Please select the best answer.
  A. C:\java\demo\src\HelloWorld.java
  B. C:\src\HelloWorld.java
  C. C:\java\HelloWorld.java
  D. C:\java\src\HelloWorld.java
  The correct answer is D. A path of ..\src\HelloWorld.java relative to C:\java\demo is equivalent to a path of src\HelloWorld.java relative to C:\java . This relative path refers to the absolute path C:\java\src\HelloWorld.java .


4. What's the maximum number of characters you should use in the name of a Java class?
Please select the best answer.
  A. 26
  B. 27
  C. 32
  D. 223
  The correct answer is A. If a class has 26 characters in its name, then the .class file will have 32 characters in its name (26 + 6). The extra 6 characters come from the necessity of appending ".class" to the end of the compiled filename. This is the maximum limit on Mac OS filenames. Windows-32 systems can handle somewhat more than this as long as the files aren't nested too deeply in a directory hierarchy. Unix systems can also handle longer filenames, but in cross-platform work you have to code to the least-common denominator.
Note that Sun often does not follow this recommendation in the class library. Among other things, this has contributed to the difficulty of porting Java to other platforms. Sun programmers were mostly Unixites who are accustomed to not worrying about filename lengths, and rarely pay more than lip service to cross-platform issues.

5. The two parts of a Macintosh file are
Please select the best answer.
  A. the information fork and the system fork
  B. the data bucket and the system bucket
  C. the data fork and the resource fork
  D. the information bucket and the resource bucket
  The correct answer is C. Mac files are divided into two forks, each of which is equivalent to a separate file on other platforms. The first part of a Mac file is called the data fork and contains the text, image data, or other basic information of the file. The second part of the file is called the resource fork and typically contains localizable strings, pictures, graphical user interface components like menu bars and dialog boxes, executable code, and more. On a Macintosh, all the standard Java I/O classes work exclusively with the data forks of Macintosh files.

6. What is one way that a file is different from a stream?
Please select the best answer.
  A. It is possible for a program to jump around in a stream, reading first one part of a stream, then another. This isn't possible with a file.
  B. It is possible for a program to jump around in a file, reading first one part of a file, then another. This isn't possible with a stream.
  C. There is no difference. A file is a type of stream and can be treated in the same way as a stream.
  D. None of the above.

The correct answer is B. A stream cannot be read in a random order. It must be read sequentially.


7. How many characters can Win32 filenames contain?
Please select the best answer.
  A. 255
  B. 8
  C. 32
  D. 65,535
  The correct answer is A. Windows filenames may contain up to 255 characters (including spaces), though room also has to be left for the path to the file. DOS filenames can only contain 8 characters with a 3-character extention. Macintosh filenames can contain up to 32 characters.