Difficulty: Easy
Correct Answer: mkdir() to create a single directory at the File path
Explanation:
Introduction / Context:
Managing directories is a basic task in Java file handling. The java.io.File class provides different methods for creating regular files and creating directories. Confusing these methods can lead to bugs or incorrect assumptions about what exists on disk. This question focuses on identifying the correct File method used to create a new directory at the path represented by a File object.
Given Data / Assumptions:
Concept / Approach:
The File class provides the method boolean mkdir() to create a single directory at the path represented by the File object. If the parent directories already exist and you have permissions, mkdir() attempts to create the directory and returns true on success. There is also mkdirs(), which can create all nonexistent parent directories in the path. In contrast, createNewFile() is used to create a regular file, and listFiles() only lists the contents of an existing directory without creating anything. The delete() method removes existing files or directories rather than creating them.
Step-by-Step Solution:
Step 1: Construct a File object pointing to the directory you want to create, for example File dir = new File("logs").Step 2: Call dir.mkdir() to request that the operating system create the directory represented by that path.Step 3: Check the boolean return value; true indicates that the directory was created, while false means the directory already existed or the creation failed.Step 4: Understand that mkdir() does not create missing parent directories; for that you would call mkdirs().Step 5: Conclude that mkdir() is the standard File method for creating a single directory at a given path, matching option A.
Verification / Alternative check:
You can confirm this behaviour by writing a small Java program that calls mkdir() and then checking the file system. The directory should appear when the method returns true. If you use createNewFile() with a path that you intend as a directory, you will get a regular file instead, and attempts to treat it as a directory will fail. The official Java documentation also describes mkdir() as creating a directory and mkdirs() as creating parent directories as necessary.
Why Other Options Are Wrong:
Option B incorrectly assigns directory creation to createNewFile(), which is intended for making regular files, not directories. Option C claims that listFiles() generates a directory, but in reality it just returns an array of File objects representing the contents of an existing directory and does not create anything. Option D suggests that delete() first creates and then removes a directory, which is completely incorrect; delete() only removes existing entries. Only option A accurately reflects the purpose and behaviour of mkdir().
Common Pitfalls:
A common pitfall is forgetting to ensure that parent directories exist before calling mkdir() or trying to use mkdir() when a complete parent path is missing; in such cases, mkdir() fails and returns false. Developers sometimes also confuse mkdir() and mkdirs(), leading to unexpected failure when creating nested directory structures. Properly checking return values and using the correct method helps avoid runtime errors when your application needs to store logs, uploads, or configuration data in specific folders.
Final Answer:
Correct answer: mkdir() to create a single directory at the File path
Discussion & Comments