Chapter 11 Quiz: Records and Variant Records

Instructions: Choose the best answer for each question. Each question has exactly one correct answer.


Question 1. Which of the following best describes why records are preferred over parallel arrays?

A) Records use less memory than parallel arrays B) Records keep related data together as a single unit, preventing accidental separation C) Records are faster to access than arrays D) Records can hold more elements than arrays


Question 2. Where should a record type definition appear in a Pascal program?

A) In the var section B) In the const section C) In the type section D) In the begin...end block


Question 3. Given the following definition, which statement correctly assigns a value to the Name field?

type
  TStudent = record
    Name: string;
    ID: Integer;
  end;
var
  S: TStudent;

A) S := 'Alice'; B) Name.S := 'Alice'; C) S.Name := 'Alice'; D) S[Name] := 'Alice';


Question 4. Which operation is NOT directly supported for record variables in Pascal?

A) Assignment (Rec2 := Rec1) B) Passing as a parameter to a procedure C) Comparison using = (if Rec1 = Rec2) D) Returning from a function


Question 5. What is the primary risk of using the WITH statement?

A) It causes a runtime error if the record is nil B) It can shadow local variables with record field names, causing ambiguity C) It makes the program run slower due to repeated lookups D) It prevents the record from being passed to procedures


Question 6. Consider the following code:

var
  A, B: TPoint;  { TPoint has X, Y: Integer }
begin
  A.X := 5;  A.Y := 10;
  B := A;
  B.X := 99;
end.

What is the value of A.X after this code runs?

A) 5 B) 99 C) 0 D) Undefined


Question 7. Given Roster: array[1..30] of TStudent;, how do you access the Name field of the third student?

A) Roster.Name[3] B) Roster[3].Name C) Name.Roster[3] D) Roster.3.Name


Question 8. Which procedure header is the most efficient way to pass a large record that will not be modified?

A) procedure Show(Student: TStudent); B) procedure Show(var Student: TStudent); C) procedure Show(const Student: TStudent); D) procedure Show(out Student: TStudent);


Question 9. In a variant record, what does the tag field do?

A) It determines the size of the entire record B) It indicates which variant's fields are currently valid C) It prevents access to the fixed fields D) It automatically converts between variant types


Question 10. How much memory does the compiler allocate for the variant part of a variant record?

A) Enough for the smallest variant B) Enough for the largest variant C) The sum of all variants D) It varies at runtime based on the tag field


Question 11. Given the following variant record:

type
  TShapeKind = (skCircle, skRect);
  TShape = record
    Color: string;
    case Kind: TShapeKind of
      skCircle: (Radius: Real);
      skRect: (Width, Height: Real);
  end;

How many end keywords close this definition?

A) 1 B) 2 C) 3 D) 0


Question 12. What happens if you set a variant record's tag to skCircle, assign Radius := 5.0, then change the tag to skRect and read Width?

A) A compile error occurs B) A runtime error occurs C) Width contains an unpredictable value (it shares memory with Radius) D) Width is automatically set to 0.0


Question 13. Which of the following is a valid nested record access?

type
  TAddress = record
    City: string;
  end;
  TPerson = record
    Home: TAddress;
  end;
var
  P: TPerson;

A) P.City := 'Portland'; B) P.Home.City := 'Portland'; C) Home.City := 'Portland'; D) P[Home].City := 'Portland';


Question 14. What is the Free Pascal convention for naming record types?

A) Prefix with R_ (e.g., R_Student) B) Prefix with T (e.g., TStudent) C) Suffix with _Rec (e.g., Student_Rec) D) All uppercase (e.g., STUDENT)


Question 15. Which of the following is true about whole-record assignment (B := A)?

A) It copies only the first field B) It copies all fields — it is a deep copy for value types C) It creates an alias so B and A refer to the same data D) It only works if the records have been initialized


Question 16. In the PennyWise project, what advantage does TExpenseRec provide over parallel arrays of amounts, categories, dates, and descriptions?

A) It uses less total memory B) Sorting expenses requires swapping only one record instead of four arrays C) It allows more than 500 expenses to be stored D) It automatically validates expense data


Question 17. A free variant record (one without a tag field) uses case Integer of. Why is this generally discouraged in normal programming?

A) It causes a compile warning B) Without a tag field, there is no way to know which variant is currently valid C) It uses more memory than a tagged variant D) It cannot be used in arrays


Question 18. Which of the following correctly initializes a record as a typed constant?

A) const S: TStudent = (Name: 'Alice'; ID: 100; GPA: 3.5; Enrolled: True); B) const S: TStudent = {Name: 'Alice', ID: 100, GPA: 3.5, Enrolled: True}; C) const S: TStudent = ['Alice', 100, 3.5, True]; D) const S: TStudent = TStudent('Alice', 100, 3.5, True);


Question 19. You need to write a function that searches an array of 1000 TEmployee records and returns the employee with the highest salary. What should the return type be?

A) Integer (return the index) B) TEmployee (return the whole record) C) Real (return the salary) D) Either A or B would work, but A is typically more efficient


Question 20. Which of Wirth's design principles is best illustrated by records?

A) Programs should minimize memory usage B) Data structures should reflect the structure of the problem domain C) Code should always prioritize execution speed D) Programs should avoid user-defined types


Answer Key

  1. B — Records keep related data together, preventing the fragile coupling of parallel arrays.
  2. C — Record types are defined in the type section.
  3. C — Dot notation: RecordVariable.FieldName.
  4. C — Pascal does not allow direct = comparison of records.
  5. BWITH can shadow local variables, making it unclear which Name is being accessed.
  6. A — Record assignment is a copy; changing B.X does not affect A.X.
  7. B — Index the array first, then use dot notation for the field.
  8. Cconst avoids copying and guarantees no modification.
  9. B — The tag field indicates which variant's fields are currently meaningful.
  10. B — The compiler allocates enough space for the largest variant.
  11. A — One end closes both the case and the record.
  12. C — Variant fields share memory; reading the wrong variant yields unpredictable data.
  13. B — Chain dot notation for nested records: P.Home.City.
  14. B — The T prefix is the universal Free Pascal convention for type names.
  15. B — Whole-record assignment copies all fields (deep copy for value types).
  16. B — With records, sorting requires swapping one entity instead of four parallel arrays.
  17. B — Without a tag, there is no programmatic way to know which variant is active.
  18. A — Typed constants use parentheses with field-name/value pairs separated by semicolons.
  19. D — Both work, but returning the index avoids copying a potentially large record.
  20. B — Records embody Wirth's principle that data structures should match the problem domain.