Chapter 34 Self-Assessment Quiz: File Formats and Serialization
Test your understanding of data formats and serialization in Pascal. Try to answer each question before revealing the answer.
Section 1: Multiple Choice
Q1. Which data format supports nested structures (objects within objects)?
(a) INI only (b) CSV only (c) JSON and XML (d) All four (INI, JSON, XML, CSV)
Answer
(c) JSON supports nesting through objects and arrays within objects. XML supports nesting through child elements. INI is flat (sections and key-value pairs only). CSV is tabular (rows and columns only).Q2. In Free Pascal's fpjson library, which method produces nicely indented JSON output?
(a) AsJSON
(b) FormatJSON
(c) PrettyPrint
(d) ToString
Answer
(b)FormatJSON produces human-readable, indented JSON. AsJSON produces compact JSON without whitespace. PrettyPrint and ToString are not fpjson methods.
Q3. What is the correct way to handle a comma inside a CSV field value?
(a) Escape it with a backslash: \,
(b) Enclose the entire field in double quotes: "Portland, OR"
(c) Replace it with a semicolon
(d) It cannot be done — commas are always delimiters
Answer
(b) The standard CSV convention is to enclose fields containing commas in double quotes. The quotes indicate that the commas within are literal characters, not field delimiters.Q4. When using TINIFile, what happens if you call ReadString for a key that does not exist?
(a) An exception is raised (b) An empty string is returned (c) The default value (third parameter) is returned (d) The program crashes
Answer
(c) AllTINIFile.Read* methods take a default value as their third parameter. If the key does not exist (or the section does not exist, or the file does not exist), the default is returned. This is a deliberate design for robustness.
Q5. Why is packed record important for binary file formats?
(a) It makes the record smaller in memory (b) It eliminates compiler-inserted padding between fields (c) It encrypts the record data (d) It enables faster file I/O
Answer
(b)packed record eliminates alignment padding that the compiler normally inserts between fields. This ensures the in-memory layout matches the intended byte layout of the file format exactly. Without packed, padding bytes would corrupt the file format.
Q6. In the fpjson library, when you add a child object to a parent object and then free the parent, what happens to the child?
(a) The child remains in memory (memory leak) (b) The child is also freed automatically (c) An access violation occurs (d) The child is set to nil
Answer
(b) When you add a JSON object/array as a child of another object/array, the parent takes ownership. Freeing the parent automatically frees all children recursively. You should NOT free child objects separately.Q7. Which format would be most appropriate for a REST API response?
(a) INI (b) Binary packed records (c) JSON (d) CSV
Answer
(c) JSON is the standard format for REST API responses. It is lightweight, supports nested structures, and is natively understood by web browsers and JavaScript. INI is for configuration, binary is for internal storage, and CSV is for tabular data export.Q8. A binary file format includes a "magic number" at the beginning. What is its purpose?
(a) To encrypt the file contents (b) To identify the file type and prevent misinterpretation (c) To compress the data (d) To store the file creation date
Answer
(b) A magic number is a fixed byte sequence that identifies the file type. Before parsing, the reader checks the magic number to ensure the file is the expected type. This prevents misinterpreting one file format as another.Section 2: True/False
Q9. JSON officially supports comments (lines starting with // or / /).
Answer
False. The JSON specification (RFC 8259) does not support comments. Some JSON parsers (like the one in VS Code settings) accept them as an extension, but standard JSON does not allow comments. If you need comments in a configuration format, consider INI or YAML instead.Q10. Free Pascal's XML DOM parser (XMLRead/XMLWrite) can read and write XML files.
Answer
True. TheXMLRead unit provides ReadXMLFile for parsing, and the XMLWrite unit provides WriteXMLFile for generating XML documents.
Q11. CSV files can represent hierarchical (nested) data structures.
Answer
False. CSV is strictly tabular — rows and columns. It cannot natively represent nesting. You could embed JSON strings in CSV cells as a workaround, but this is not standard CSV and requires custom parsing.Section 3: Short Answer
Q12. You are designing a binary file format for storing 1 million temperature readings (timestamp + temperature value). Each reading is a TDateTime (8 bytes) and a Double (8 bytes). Calculate the expected file size, excluding the header. Why is binary the best choice here?
Answer
Each record is 16 bytes (8 + 8). One million records: 16 × 1,000,000 = 16,000,000 bytes = ~15.3 MB. In JSON, each record might be 60+ bytes ("timestamp": "...", "temp": 23.5), giving ~60 MB. In CSV, each row might be 30 bytes, giving ~30 MB. Binary is best because: (1) smallest file size, (2) fastest read/write (direct memory copy vs. parsing), (3) no conversion needed (native format matches memory layout), (4) for 1M records, the performance difference is significant.Q13. Explain the serializer interface pattern from Section 34.7. Why is it better than writing separate export functions that the main program calls directly?
Answer
The serializer interface (IExpenseSerializer) defines a common contract for saving/loading expenses regardless of format. Each format implements the interface (TJSONExpenseSerializer, TCSVExpenseSerializer, etc.). The advantage: the main program works with the interface, not specific format classes. Adding a new format (e.g., YAML) requires only creating a new class — no changes to existing code. This follows the Open/Closed Principle (open for extension, closed for modification) and the dependency inversion principle.Q14. A CSV file uses semicolons as delimiters, periods as thousands separators, and commas as decimal separators (European convention). The amount field contains "1.234,50". Describe the steps to convert this to a Pascal Currency value.