Programming Constructs and Operators: Question 9
Syllabus 8.1
A library system stores four pieces of data about each book: how many pages it has, its
average reader rating out of 5 (for example 4.5), a single-letter genre code (for example
'F' for fiction), and whether it is currently on loan.
Which set of DECLARE statements uses the most appropriate data type for each of these four
pieces of data?
Show worked solution Hide worked solution
Worked solution
Step 1: Choose a data type for each piece of data
- Pages is a whole number of pages. There is no such thing as half a page in a page count,
so the most appropriate type is
INTEGER. - Rating is an average out of 5 that can include a decimal value such as
4.5, so it needs a type that can store fractional values:REAL. - GenreCode is described as a single letter, such as
'F'. A single character is exactly what theCHARtype is for;STRINGwould also technically hold one character, butCHARis the most appropriate and precise type for data that is always exactly one character long. - OnLoan can only be one of two states, true or false, so the most appropriate type is
BOOLEAN.
Step 2: Match this to option A
Pages : INTEGER, Rating : REAL, GenreCode : CHAR, OnLoan : BOOLEAN
This is exactly option A.
Step 3: Why the other options are wrong
- Option B swaps
INTEGERandREAL: it declaresPagesasREALandRatingasINTEGER. This is backwards. A page count is always a whole number (INTEGER), while an average rating like4.5needs decimal places (REAL). - Option C declares
GenreCodeasSTRINGrather thanCHAR. Since the genre code is always exactly one character,CHARis the more precise and appropriate choice;STRINGis intended for text of any length, which is more than a single-letter code needs. - Option D declares
OnLoanasCHARrather thanBOOLEAN. Representing a true/false value with a character (for example'Y'or'N') works in principle, butBOOLEANis the dedicated data type for values that can only beTRUEorFALSE, and is the most appropriate choice here.
Final answer
- The most appropriate data types are
INTEGER,REAL,CHARandBOOLEAN, option A.