Programming Constructs and Operators: Question 9

Syllabus 8.1

Multiple choice 1 mark

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?

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

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 the CHAR type is for; STRING would also technically hold one character, but CHAR is 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 INTEGER and REAL: it declares Pages as REAL and Rating as INTEGER. This is backwards. A page count is always a whole number (INTEGER), while an average rating like 4.5 needs decimal places (REAL).
  • Option C declares GenreCode as STRING rather than CHAR. Since the genre code is always exactly one character, CHAR is the more precise and appropriate choice; STRING is intended for text of any length, which is more than a single-letter code needs.
  • Option D declares OnLoan as CHAR rather than BOOLEAN. Representing a true/false value with a character (for example 'Y' or 'N') works in principle, but BOOLEAN is the dedicated data type for values that can only be TRUE or FALSE, and is the most appropriate choice here.

Final answer

  • The most appropriate data types are INTEGER, REAL, CHAR and BOOLEAN, option A.