Advanced Data Representation and File Organisation: Question 10

Syllabus 13.1

Multiple choice A2 1 mark

A program needs to store which programming languages a particular student has ever used, so that the program can quickly check whether a given language, such as Python, is among them. The order in which the student first used each language does not matter, and if the student has used a language more than once, it should still only be counted once, never held twice over.

Which composite user-defined data type is most suitable for storing one student's languages?

Choose an answer to check it, then compare with the worked solution below.

Show worked solution Hide worked solution

Worked solution

Matching the requirement to a composite data type

The requirement has two defining features: (1) each language must be held at most once, no matter how many times it was actually used, and (2) the order in which languages were first used is not important.

A set is exactly the composite user-defined type designed for this: it holds an unordered collection of values, and automatically ensures each distinct value it contains is only ever held once, adding a value already present in the set simply leaves the set unchanged.

DECLARE StudentLanguages : SET OF STRING
StudentLanguages ← {}
StudentLanguages ← StudentLanguages ∪ {"Python"}
StudentLanguages ← StudentLanguages ∪ {"Python"}   // no effect - "Python" is already present

After both lines run, StudentLanguages still contains "Python" only once, and checking whether "Python" is a member of StudentLanguages can be done directly, without caring in which order it was originally added.

Why the other options are wrong

  • A (array): an array stores values at specific, ordered index positions, and the same value can legally be stored at more than one index. Neither “no meaningful order” nor “each value held only once” is guaranteed by an array.
  • B (record): a record groups a fixed number of named fields, often of different types, into one variable (for example, a student’s name and age together). It is not designed to hold a variable-sized collection of same-type values with automatic uniqueness.
  • D (enumerated type): an enumerated type defines a fixed list of possible named values once, when the type itself is created (for example, the type Language = (Python, Java, C)). It does not represent a changeable collection that grows as a particular student uses more languages.

Final answer

C. A set is the correct choice, because it holds an unordered collection of values in which each distinct value is automatically guaranteed to appear only once.