1Z0-830 practice question 8 of 25

When compiling unnamed modules, which directory structure is recommended for source files?

Full set: 116 questions
Answer options
A:

A. All source files in the same directory.

B:

B. Source files organized by package but all under one root directory.

Correct answer
C:

C. Each source file in its own directory.

D:

D. No specific structure is required.

Correct Answer:
B. B. Source files organized by package but all under one root directory.
Explanation

The standard and recommended convention for organizing Java source files, which applies to code compiled for both named and unnamed modules, is to mirror the package structure in the file system. A root source directory (e.g., src) contains a hierarchy of subdirectories corresponding to the package names. For example, a class com.example.MyClass should be in the file src/com/example/MyClass.java. This structure allows the compiler and build tools to locate source files predictably.

Why Incorrect

A. Placing all source files in one directory is not scalable and leads to name collisions for classes in different packages.
C. Placing each source file in its own directory is unnecessarily complex and violates the standard package-to-directory mapping convention.
D. While the compiler can be passed an explicit list of files, a consistent structure is required for any maintainable project and is expected by all standard Java tools.

References

1. Oracle Corporation. (2023). The Java™ Tutorials: Creating and Using Packages. In the "Essential Java Classes" trail, "Packages" lesson. "The package name and the directory structure must match."

2. Oracle Corporation. (2023). javac - Java programming language compiler. In Java Development Kit 21 Tool Specifications. The documentation for -sourcepath and -d options implicitly relies on this package-based directory structure to locate and place files.

3. Stroustrup, B. (2014). Programming: Principles and Practice Using C++ (2nd ed.). Addison-Wesley. (While for C++, the principle of mapping namespaces/packages to directory structures for project organization is a widely accepted software engineering practice, also taught in university courses like MIT's 6.005).

Scroll to Top