Chapter 12 Quiz: Sets and Enumerations

Test your understanding of enumerated types, subrange types, and sets. Choose the best answer for each question.


Question 1. Given the declaration type TColor = (Red, Green, Blue);, what is the value of Ord(Green)?

  • (A) 0
  • (B) 1
  • (C) 2
  • (D) It depends on the compiler

Answer: (B) Ordinal values start at 0 for the first element. Red = 0, Green = 1, Blue = 2.


Question 2. Which of the following is a valid subrange type declaration?

  • (A) type TPercent = 0.0..100.0;
  • (B) type TPercent = 0..100;
  • (C) type TPercent = 'A'..'z';
  • (D) Both (B) and (C)

Answer: (D) Subranges can be declared over any ordinal type, including integers and characters. Real numbers are not ordinal, so (A) is invalid. Both (B) and (C) are valid subrange types.


Question 3. What does the expression [1, 3, 5] + [2, 4, 5] evaluate to?

  • (A) [1, 2, 3, 4, 5, 5]
  • (B) [1, 2, 3, 4, 5]
  • (C) [5]
  • (D) [1, 3]

Answer: (B) The + operator computes the union of two sets. Sets contain no duplicates, so 5 appears only once.


Question 4. What does the expression [1, 2, 3, 4] * [3, 4, 5, 6] evaluate to?

  • (A) [1, 2, 3, 4, 5, 6]
  • (B) [3, 4]
  • (C) [1, 2]
  • (D) []

Answer: (B) The * operator computes the intersection — elements present in both sets. Only 3 and 4 appear in both sets.


Question 5. Which statement correctly tests whether a character Ch is a digit?

  • (A) if Ch = ['0'..'9'] then
  • (B) if Ch in ['0'..'9'] then
  • (C) if ['0'..'9'] in Ch then
  • (D) if Ch of ['0'..'9'] then

Answer: (B) The in operator tests set membership. The element goes on the left, the set on the right.


Question 6. Given type TSeason = (Spring, Summer, Autumn, Winter);, what is the result of Succ(Winter)?

  • (A) Spring
  • (B) A compile-time error
  • (C) A runtime error (with range checking on)
  • (D) 0

Answer: (C) Winter is the last value in the enumeration. Succ of the last value causes a runtime range check error when {$R+} is enabled. Without range checking, the behavior is undefined.


Question 7. What does [1, 2, 3] - [2, 4] evaluate to?

  • (A) [1, 3]
  • (B) [1, 2, 3, 4]
  • (C) [4]
  • (D) [-1, -1]

Answer: (A) The - operator computes set difference — elements in the first set that are not in the second. 1 and 3 are in [1,2,3] but not in [2,4].


Question 8. What is the maximum number of elements a Pascal set can contain (in Free Pascal)?

  • (A) 32
  • (B) 64
  • (C) 128
  • (D) 256

Answer: (D) Pascal sets are implemented as bit vectors. In Free Pascal, the base type's ordinal values must be in the range 0..255, giving a maximum of 256 elements.


Question 9. Which declaration is INVALID?

  • (A) type TSmallSet = set of 0..31;
  • (B) type TCharSet = set of Char;
  • (C) type TIntSet = set of Integer;
  • (D) type TBoolSet = set of Boolean;

Answer: (C) Integer has far more than 256 possible values, so set of Integer is invalid. All other base types have 256 or fewer possible values.


Question 10. Given var S: set of 1..10; and S := [2, 4, 6, 8];, what is the value of 5 in S?

  • (A) True
  • (B) False
  • (C) 5
  • (D) A compile error

Answer: (B) The value 5 is not in the set [2, 4, 6, 8], so the in expression evaluates to False.


Question 11. What is the purpose of the {$R+} compiler directive?

  • (A) It enables record field alignment
  • (B) It enables runtime range checking
  • (C) It enables read-only mode for variables
  • (D) It enables recursive function support

Answer: (B) {$R+} enables runtime range checking. When active, assigning an out-of-range value to a subrange type causes a runtime error instead of silent corruption.


Question 12. Which of the following correctly adds pWrite to a set variable Perms?

  • (A) Perms.Add(pWrite);
  • (B) Include(Perms, pWrite);
  • (C) Perms := Perms + pWrite;
  • (D) Perms := Perms or pWrite;

Answer: (B) The Include procedure adds a single element to a set. Option (C) is almost correct but needs brackets: Perms := Perms + [pWrite]. Option (A) uses object syntax which does not apply to set types.


Question 13. What does the expression [Monday, Tuesday] <= [Monday..Friday] evaluate to?

  • (A) True
  • (B) False
  • (C) A compile error
  • (D) It depends on the ordinal values

Answer: (A) The <= operator tests whether the left set is a subset of (or equal to) the right set. [Monday, Tuesday] is indeed a subset of [Monday..Friday].


Question 14. Given type TDay = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);, which is valid for an array declaration?

  • (A) var Hours: array[TDay] of Integer;
  • (B) var Hours: array[0..6] of Integer;
  • (C) var Hours: array[Mon..Sun] of Integer;
  • (D) All of the above

Answer: (D) All three are valid. (A) uses the enumerated type as the index, (B) uses an integer range, and (C) uses a subrange of the enumerated type. All result in an array with 7 elements.


Question 15. How is a set of TDay (7 elements) stored internally?

  • (A) As a linked list of elements
  • (B) As a hash table
  • (C) As a bit vector (1 byte minimum)
  • (D) As an array of Boolean values

Answer: (C) Pascal sets are stored as bit vectors. A set of TDay requires only 7 bits, which fits in a single byte (though it may be padded to 4 bytes for alignment on some platforms).


Question 16. What is the advantage of Include(S, x) over S := S + [x]?

  • (A) Include works on any type; + only works on integers
  • (B) Include modifies the set in place without constructing a temporary set
  • (C) Include can add multiple elements at once
  • (D) There is no practical difference

Answer: (B) Include directly modifies a single bit in the set variable, while S := S + [x] constructs a temporary single-element set and then performs a union. Include is slightly more efficient, especially in loops.


Question 17. Given type TMonth = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);, what does Low(TMonth) return?

  • (A) 0
  • (B) 1
  • (C) Jan
  • (D) 'Jan'

Answer: (C) Low(TMonth) returns the first value of the enumerated type, which is Jan. It returns the enumeration value, not its ordinal number and not a string.


Question 18. Which set operation would you use to determine whether two sets have any elements in common?

  • (A) if A + B = [] then — they share elements
  • (B) if A * B <> [] then — they share elements
  • (C) if A - B = [] then — they share elements
  • (D) if A = B then — they share elements

Answer: (B) The intersection A * B contains elements common to both sets. If it is not empty (<> []), the sets share at least one element.


Question 19. What problem does the {$scopedenums on} directive solve?

  • (A) It allows sets to exceed the 256-element limit
  • (B) It prevents name collisions between different enumerated types
  • (C) It enables enumerations to be written to files
  • (D) It allows enumerations to have string values

Answer: (B) With scoped enumerations, values must be qualified with their type name (e.g., TColor.Red), which prevents collisions when multiple enumerations share value names.


Question 20. In the PennyWise project, why do we prefix enumeration values like ecFood instead of just Food?

  • (A) Pascal requires all enumeration values to start with lowercase letters
  • (B) It prevents name collisions with identifiers in other modules
  • (C) It makes the compiler generate more efficient code
  • (D) It is required for the set of syntax to work

Answer: (B) The ec prefix (for "expense category") is a naming convention that prevents collisions. Without scoped enumerations, bare names like Food could conflict with variable names, function names, or values from other enumerations.