Chapter 33 Self-Assessment Quiz: Units, Packages, and Modular Design
Test your understanding of Pascal units and modular design. Try to answer each question before revealing the answer.
Section 1: Multiple Choice
Q1. In a Pascal unit, which section contains the public declarations visible to other units?
(a) The implementation section (b) The interface section (c) The initialization section (d) The uses clause
Answer
(b) The interface section contains all public declarations — types, constants, variables, and procedure/function headers. Everything in the implementation section is private to the unit.Q2. Which of the following is permitted in a unit's interface section?
(a) A complete procedure body with begin..end
(b) A procedure header without a body
(c) A WriteLn statement
(d) A variable assignment
Answer
(b) The interface section can contain declarations only — procedure and function headers without bodies, type declarations, constant declarations, and variable declarations. No executable code (procedure bodies, statements) is allowed.Q3. If Unit A's interface uses Unit B, and Unit B's interface uses Unit A, the result is:
(a) A compilation error — circular reference (b) Valid — the compiler resolves it automatically (c) A runtime error (d) The units compile but cannot be linked
Answer
(a) Circular references in interface uses clauses produce a compilation error. The compiler cannot determine which interface to process first. The solution is to move one of the dependencies to an implementation uses clause.Q4. When a program uses three units (A, B, C) and all three have initialization sections, in what order do they execute?
(a) Alphabetical order (b) Random order (c) Dependency order — units depended upon initialize first (d) Reverse dependency order
Answer
(c) Units initialize in dependency order. If B depends on A, then A initializes before B. This guarantees that by the time a unit's initialization runs, all units it depends on are already initialized. Finalization runs in the reverse order.Q5. What is the primary advantage of placing a dependency in the implementation uses clause rather than the interface uses clause?
(a) It compiles faster (b) It reduces transitive dependencies and can break circular references (c) It makes the dependency optional (d) It prevents name collisions
Answer
(b) Interface dependencies are transitive — if A's interface uses B, any unit using A also indirectly depends on B. Implementation dependencies are not transitive. Additionally, circular references between interface uses clauses are prohibited, but you can resolve them by moving one to the implementation uses clause.Q6. A unit file named MathHelpers.pas must declare itself as:
(a) program MathHelpers;
(b) unit MathHelpers;
(c) module MathHelpers;
(d) library MathHelpers;
Answer
(b) The unit name in the source code must match the filename.unit MathHelpers; must be in a file named MathHelpers.pas. The library keyword is used for dynamic libraries, not units. module is not a Pascal keyword.
Q7. Which design principle states that a module's elements should be closely related and focused on a single purpose?
(a) Coupling (b) Encapsulation (c) Cohesion (d) Polymorphism
Answer
(c) Cohesion measures how closely related and focused the elements within a module are. High cohesion means the module does one thing well. Low cohesion means it is a grab-bag of unrelated functionality.Q8. To resolve a name conflict between two units that both export a Process procedure, you should:
(a) Rename one of the procedures
(b) Use a qualified identifier: UnitName.Process
(c) Remove one of the units from the uses clause
(d) Use the override keyword
Answer
(b) Qualified identifiers —UnitA.Process and UnitB.Process — unambiguously specify which unit's procedure to call. This is Pascal's standard mechanism for resolving name conflicts.
Section 2: True/False
Q9. A unit can have an initialization section without a finalization section.
Answer
True. A unit can have initialization only, finalization only, both, or neither. They are independent.Q10. Private helper functions declared in a unit's implementation section can be called from other units that use it.
Answer
False. Functions declared only in the implementation section are private to the unit. Other units cannot see or call them — they can only access what is declared in the interface section.Q11. Dynamic libraries are linked into the executable at compile time.
Answer
False. Dynamic libraries (DLLs / shared objects) are loaded at runtime. Static libraries are linked at compile time.Q12. If no units have interface-level circular dependencies, the unit dependency graph is guaranteed to be a directed acyclic graph (DAG).
Answer
True. Since circular interface dependencies cause a compilation error, any compiling set of units must have an acyclic interface dependency graph. Implementation dependencies may create cycles, but these do not affect the compilation order of interface sections.Section 3: Short Answer
Q13. A developer creates a unit called DataProcessor and exports 45 functions in the interface. What design problem does this suggest, and how would you address it?
Answer
This suggests low cohesion — the unit is likely doing too many unrelated things. 45 public functions is a very large interface. The solution is to split the unit into multiple smaller, more focused units, each handling one responsibility. For example, DataParser, DataValidator, DataTransformer, DataExporter might each have 8-12 focused functions.Q14. Explain why FinanceCore in the PennyWise checkpoint has no dependencies on FinanceDB or FinanceUI.
Answer
FinanceCore contains the domain model — the types and operations that define what expenses and budgets ARE, independent of how they are stored or displayed. By keeping FinanceCore free of database and UI dependencies, it can be reused in any context (command-line, GUI, web), tested in isolation, and changed without affecting storage or display code. This follows the dependency inversion principle: high-level policy (domain model) should not depend on low-level details (storage, UI).Q15. What files does Free Pascal produce when you compile a unit? What is each file used for?
Answer
Free Pascal produces: (1) A.ppu file — the compiled unit interface, used by the compiler when other units reference this one. (2) A .o file — the compiled object code (implementation), used by the linker to include the unit's code in the final executable. The original .pas file is the source code. If you distribute a unit without source, you provide the .ppu and .o files.