Java OCA OCP Practice Question 1882

Question

Given the following class, three of the values ensure it runs properly on various different systems.

Which value does not?

package mypkg; //from  w  ww. jav  a2  s  . c o m
import java.io.*; 
public class Main { 
  private final String directory; 
  public Main(String directory) { 
     this.directory = directory; 
  } 
  public File getDatabaseFolder(String file) { 
     return new File(directory + ___ + file); 
  } 
} 
A.   java.io.File.separator
B.  new File(new String()).separatorChar
C.   System.getProperty("file.separator")
D.  System.getProperty("path.separator")


D.

Note

The file name separator is often a forward-slash (/) in Linux-based systems and a backward-slash (\) in Windows-based systems.

Option A is valid because it is the fully qualified name of the property.

Option B is also valid because File.separator and File.separatorChar are equivalent.

While accessing a static variable using an instance is discouraged, as shown in Option B, it is allowed.

Option C is valid and a common way to read the character using the System class.

Finally, Option D is the correct answer and one call that cannot be used to get the system-dependent name separator character.

System.getProperty("path.separator") is used to separate sets of paths, not names within a single path.




PreviousNext

Related