Quiz: Advanced Object Pascal
Instructions: Choose the best answer for each question. Answers are provided at the end.
Q1. What is operator overloading?
A) Calling an operator more times than it can handle B) Defining the behavior of standard operators for custom types C) Creating new operators that do not exist in the language D) Overriding methods in a subclass
Q2. Which keyword is used to declare an operator for an advanced record in Free Pascal?
A) function
B) override
C) class operator
D) overload
Q3. When is operator overloading a BAD idea?
A) When defining + for vector addition
B) When defining * for scalar multiplication of a vector
C) When defining + for a TUser class (meaning is ambiguous)
D) When defining = for comparing two dates
Q4. What is a class helper?
A) A class that inherits from another class B) A construct that adds new methods to an existing class without modifying its source C) A class that contains only helper procedures D) A design pattern for dependency injection
Q5. How many class helpers can be active for a single class at a time?
A) Unlimited B) Two — one for methods, one for properties C) One per scope D) None — class helpers are not supported
Q6. What can a class helper NOT do?
A) Add new methods B) Add new data fields to the target class C) Add new properties D) Access existing public methods of the target class
Q7. What is an anonymous function?
A) A function whose name is hidden from the debugger B) A function defined inline without a name, often used as a callback C) A function that takes no parameters D) A private function invisible to other units
Q8. What is a closure?
A) A function that closes a file B) An anonymous function that captures variables from its enclosing scope C) A method that marks an object as finalized D) A function that returns nothing
Q9. What does the reference to type qualifier enable?
A) Pass-by-reference parameters B) Reference counting for objects C) Holding closures (anonymous functions with captured variables) in function variables D) Creating circular references
Q10. What does RTTI stand for?
A) Real-Time Type Inspection B) Run-Time Type Information C) Record Type Type Inference D) Reference-Typed Type Instance
Q11. When is RTTI most appropriate?
A) In every method of every class B) In framework code like serialization, form designers, and plugin systems C) For simple variable assignments D) For replacing all type checks
Q12. In a fluent interface, what do configuration methods typically return?
A) nil
B) A new object
C) Self (the same object)
D) A Boolean indicating success
Q13. Which pattern is demonstrated by this code?
Query := TQueryBuilder.Create
.From('expenses')
.Where('amount > 50')
.OrderBy('date')
.Build;
A) Observer pattern B) Strategy pattern C) Fluent interface / Builder pattern D) Template method pattern
Q14. What is the advantage of using function(X: Integer): Integer begin Result := X * 2; end instead of defining a named function DoubleIt?
A) It runs faster B) It is more readable when used as a short, one-time callback at the point of use C) It uses less memory D) It avoids all type checking
Q15. Which of the following best captures the chapter's message about advanced features?
A) Use every advanced feature as often as possible to demonstrate expertise B) Avoid all advanced features in favor of basic constructs C) Use advanced features when they make code clearer; avoid them when they make code clever D) Advanced features are only for library authors, not application developers
Answer Key
| Q | Answer | Explanation |
|---|---|---|
| 1 | B | Operator overloading defines the meaning of +, -, <, etc. for custom types. |
| 2 | C | class operator is the syntax for declaring operators inside advanced records. |
| 3 | C | If the operator's meaning is ambiguous (User + User?), use a named method instead. |
| 4 | B | Class helpers add methods to existing classes without modifying them or using inheritance. |
| 5 | C | Only one class helper per type can be active at a time in a given scope. |
| 6 | B | Class helpers cannot add data fields — only methods and properties. |
| 7 | B | Anonymous functions are unnamed, inline function definitions often used as callbacks. |
| 8 | B | A closure is an anonymous function that captures variables from the enclosing scope. |
| 9 | C | reference to enables function variables to hold closures, not just plain function pointers. |
| 10 | B | RTTI = Run-Time Type Information (also called reflection). |
| 11 | B | RTTI is appropriate for framework-level code, not normal application logic. |
| 12 | C | Fluent methods return Self to enable method chaining. |
| 13 | C | Method chaining with a final Build call is the fluent interface/builder pattern. |
| 14 | B | Anonymous functions are most useful for short, one-time callbacks at the call site. |
| 15 | C | The chapter's core message: use features for clarity, not cleverness. |