Advanced Data Representation and File Organisation: Question 1
Syllabus 13.1
A quiz app stores each question's difficulty level. A difficulty level can only ever be one of
four fixed named values: Easy, Medium, Hard or Expert. No other value is ever valid.
Which user-defined data type should be used to store a single question's difficulty level?
Show worked solution Hide worked solution
Worked solution
Matching the requirement to a user-defined type
The syllabus splits user-defined data types into non-composite types (enumerated, pointer) and composite types (set, record, class/object). The right choice always depends on what the variable actually needs to hold.
Here, a difficulty level must be restricted to exactly one of four fixed, named values.
Easy, Medium, Hard, Expert, and nothing else should ever be a valid value. This is
precisely what an enumerated data type is designed for: it defines a fixed, ordered list of
named values, and any variable declared with that type can only ever be assigned one of those
listed names.
In pseudocode, this would be declared as:
TYPE DifficultyLevel = (Easy, Medium, Hard, Expert)
DECLARE QuestionDifficulty : DifficultyLevel
QuestionDifficulty ← Medium
Because DifficultyLevel only permits these four named values, an attempt to assign anything
else (e.g. QuestionDifficulty ← Easey) would be rejected, a level of built-in validation a plain
STRING variable cannot offer.
Why the other options are wrong
- A (STRING): a STRING variable can be assigned any sequence of characters, including invalid or misspelt values. It does not restrict the variable to the four intended values.
- C (record): a record is a composite type that groups several different fields together (e.g. question text, difficulty, marks) into one variable. It solves a different problem, grouping related values, not restricting a single value to a fixed list.
- D (pointer): a pointer holds the memory address of another variable, so that variable can be accessed or modified indirectly. A difficulty level is not a reference to another variable. It is one value chosen from a fixed list, which is exactly the enumerated type’s job.
Final answer
B. An enumerated data type is the correct choice, because it restricts a variable to one
value from a small, fixed set of named values, exactly matching the requirement for a difficulty
level of Easy, Medium, Hard or Expert.