Quiz: Generics
Instructions: Choose the best answer for each question. Answers are provided at the end.
Q1. What problem do generics primarily solve?
A) Making programs run faster B) Eliminating the need to write type-specific versions of the same algorithm or data structure C) Replacing all classes with records D) Providing garbage collection
Q2. In Free Pascal {$mode objfpc}, which keyword is used to declare a generic class?
A) template
B) parameterized
C) generic
D) abstract
Q3. In Free Pascal {$mode objfpc}, which keyword creates a concrete type from a generic?
A) instantiate
B) specialize
C) implement
D) derive
Q4. What does the type constraint T: class mean?
A) T must be a record type B) T must be a class (descendant of TObject) C) T must have a Free method D) T must be an interface
Q5. Which unit provides TFPGList<T>, TFPGObjectList<T>, and TFPGMap<K,V>?
A) SysUtils B) Classes C) fgl D) Generics.Collections
Q6. What is the difference between TFPGList<T> and TFPGObjectList<T>?
A) TFPGList<T> is faster
B) TFPGObjectList<T> can optionally free its items when they are removed
C) TFPGList<T> supports sorting but TFPGObjectList<T> does not
D) There is no difference
Q7. What happens if you try to add a String to a variable of type specialize TFPGList<Integer>?
A) The string is converted to an integer automatically B) A runtime exception is raised C) A compile-time error occurs D) The string is silently discarded
Q8. Why are generics safer than using TList with Pointer casts?
A) Generics are faster at runtime B) Generics enforce type safety at compile time, preventing type confusion C) Generics use less memory D) Generics work with older Pascal compilers
Q9. In a generic class generic TStack<T>, what is T called?
A) A type constraint B) A type parameter C) A type specialization D) A type variable
Q10. Which of the following is a valid type constraint in Free Pascal?
A) T: integer
B) T: class
C) T: dynamic
D) T: static
Q11. What does FreeObjects := True do on a TFPGObjectList<T>?
A) Frees the list itself B) Automatically frees contained objects when they are removed or the list is destroyed C) Prevents objects from being added D) Converts objects to pointers
Q12. How does the compiler handle a generic specialization like specialize TStack<Integer>?
A) It interprets the generic code at runtime B) It generates type-specific compiled code at compile time C) It uses dynamic dispatch to route method calls D) It wraps all values in Variant types
Q13. Which approach to reusable data structures provides BOTH type safety AND code reuse?
A) Pointer-based TList B) Variant arrays C) Code duplication (separate class per type) D) Generics
Q14. What is the Delphi-mode syntax for declaring a generic class (without generic/specialize keywords)?
A) TStack<T> = class ... end;
B) template TStack(T) = class ... end;
C) TStack[T] = class ... end;
D) generic TStack<T> = class ... end;
Q15. A generic function Max<T>(A, B: T): T that uses the > operator requires:
A) No constraint — > works for all types
B) A type constraint ensuring T supports comparison, or a comparison function parameter
C) T to be explicitly cast to Integer before comparison
D) The comparable keyword
Answer Key
| Q | Answer | Explanation |
|---|---|---|
| 1 | B | Generics let you write one data structure or algorithm that works with any type. |
| 2 | C | In {$mode objfpc}, the generic keyword precedes generic type declarations. |
| 3 | B | The specialize keyword creates a concrete type from a generic in {$mode objfpc}. |
| 4 | B | T: class constrains T to be a class type (descendant of TObject). |
| 5 | C | The fgl unit (Free Pascal Generic Library) provides built-in generic collections. |
| 6 | B | TFPGObjectList<T> has a FreeObjects property to automatically free items on removal. |
| 7 | C | Generics enforce type safety at compile time — type mismatches are compile errors. |
| 8 | B | Generics catch type confusion at compile time; pointer-based lists defer errors to runtime. |
| 9 | B | T is a type parameter — a placeholder for a concrete type. |
| 10 | B | T: class is a valid constraint. Others include constructor, record, and interface types. |
| 11 | B | When FreeObjects is True, removing or destroying the list also frees contained objects. |
| 12 | B | The compiler generates specialized code for each concrete type at compile time (reification). |
| 13 | D | Generics provide both type safety (compile-time checking) and code reuse (one definition, many types). |
| 14 | A | In {$mode delphi}, generics use bare angle brackets without generic/specialize keywords. |
| 15 | B | Not all types support >. You need either a constraint or a comparison function parameter. |