Quiz: Interfaces, Abstract Classes, and Design Principles

Instructions: Choose the best answer for each question. Answers are provided at the end.


Q1. Which of the following best describes an interface in Object Pascal?

A) A class with all methods implemented B) A pure contract defining method signatures with no implementation C) A class that can be instantiated directly D) A data type that holds fields and methods


Q2. What is the root interface from which all Object Pascal interfaces implicitly descend?

A) TObject B) IUnknown C) IInterface D) Both B and C (they are aliases)


Q3. Why must interfaces have a GUID?

A) The compiler cannot parse interfaces without one B) It enables runtime querying with Supports() and as C) It determines the interface's memory layout D) It provides automatic serialization


Q4. What class should you typically inherit from when implementing interfaces?

A) TObject B) TComponent C) TInterfacedObject D) TPersistent


Q5. What happens when the last interface reference to a TInterfacedObject instance goes out of scope?

A) Nothing — you must call Free manually B) The object is automatically freed via reference counting C) The program raises an exception D) A memory leak occurs


Q6. Which of the following can an interface contain?

A) Data fields B) A constructor C) Method signatures and property declarations D) Concrete method implementations


Q7. A class TDocument already inherits from TCustomControl. You want it to also be exportable and printable. What is the best approach?

A) Use multiple inheritance from TCustomControl, TExporter, and TPrinter B) Define IExportable and IPrintable interfaces and have TDocument implement both C) Copy the export and print code directly into TDocument D) Create a helper class and use friend access


Q8. What is a method resolution clause used for?

A) To resolve circular unit dependencies B) To map an interface method to a differently-named class method when two interfaces share method names C) To determine method call order in deep inheritance hierarchies D) To override abstract methods in a subclass


Q9. Which SOLID principle states: "No client should be forced to depend on methods it does not use"?

A) Single Responsibility Principle B) Open/Closed Principle C) Interface Segregation Principle D) Dependency Inversion Principle


Q10. Which SOLID principle states: "High-level modules should not depend on low-level modules; both should depend on abstractions"?

A) Single Responsibility Principle B) Liskov Substitution Principle C) Interface Segregation Principle D) Dependency Inversion Principle


Q11. In the Template Method pattern, where is the algorithm skeleton defined?

A) In an interface B) In a concrete method of an abstract base class C) In each subclass independently D) In a separate configuration file


Q12. Which design pattern encapsulates a family of algorithms behind an interface, making them interchangeable at runtime?

A) Observer B) Template Method C) Strategy D) Singleton


Q13. When should you prefer an abstract class over an interface?

A) When you need multiple implementation across unrelated hierarchies B) When you need to share code and state among closely related classes C) When you want automatic reference counting D) When the implementing classes have no shared behavior


Q14. What does the Supports function return?

A) The object's class name B) A Boolean indicating whether an object implements a specific interface C) The interface's GUID D) The reference count of the object


Q15. Consider: constructor Create(AExporter: IExportable). This is an example of which technique?

A) Operator overloading B) Dependency injection C) Method resolution D) Template method


Answer Key

Q Answer Explanation
1 B Interfaces define method signatures only — no implementation, no fields.
2 D IInterface and IUnknown are aliases for the same root interface.
3 B The GUID enables runtime type querying with Supports(), as, and QueryInterface.
4 C TInterfacedObject provides default implementations of _AddRef, _Release, and QueryInterface.
5 B TInterfacedObject uses reference counting; when the count reaches zero, the object is freed.
6 C Interfaces can declare method signatures and properties (via getters/setters) but not fields, constructors, or implementations.
7 B Pascal supports single inheritance only, so interfaces are the way to add multiple capabilities.
8 B Method resolution clauses map interface methods to specific class methods, resolving name conflicts.
9 C Interface Segregation — keep interfaces small and focused.
10 D Dependency Inversion — depend on abstractions (interfaces), not concrete implementations.
11 B The Template Method pattern uses a concrete method in the base class that calls abstract methods overridden by subclasses.
12 C The Strategy pattern encapsulates algorithms behind an interface for runtime interchangeability.
13 B Abstract classes shine when related classes share code and state; interfaces are better for cross-hierarchy contracts.
14 B Supports returns True if the object implements the interface, and optionally stores the reference.
15 B Passing an interface through a constructor parameter is dependency injection.