OCR A-Level Computer Science H446
Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation
Software Development
Types of Programming Language
Exchanging Data
Networks
Web Technologies
Data Types and Number Representation
Data Structures
Boolean Algebra
Legal, Moral, Cultural and Ethical Issues
Elements of Computational Thinking
Problem Solving and Programming
Algorithms
Data Types and Number Representation
Primitive data types
- 3 things to know
- Component 01
Overview
Recall questions on integer, real, Boolean, character and string types, why 0.1 cannot be stored exactly, testing reals for equality, storing prices, concatenation versus addition, integer division and modulus, casting and overflow.
What you need to know
Answer each one before you reveal it.
A shop program stores prices. Would you use a real to hold 19.99, and why?
In a language where + works on both, what is the difference between 5 + 3 and "5" + "3"?
Questions from this subtopic
Each one with the model answer underneath.
- 1
What does a variable's data type actually tell the computer?
Model answer
Two things. First, how much memory to set aside for the variable. Second, how to interpret the bit pattern stored there, and therefore which operations are legal on it and what those operations mean. The same eight bits could be a small integer, part of a real number, or a character code; only the type decides which.
- 2
Name the five primitive data types and state, in one line each, what they store.
Model answer
Integer: a whole number, positive or negative, with no fractional part. Real (also called floating point): a number that may have a fractional part. Character: a single symbol, stored as its code number from a character set. String: a sequence of characters of any length, including zero. Boolean: one of exactly two values, TRUE or FALSE.
- 3
Why is 'primitive' the right word for these types?
Model answer
They are the built-in types the language provides directly, rather than types the programmer composes out of other types. Arrays, records and objects are built from primitives; primitives are not built from anything smaller within the language.
- 4
An integer and a real number are both numbers. Why bother having two types?
Model answer
They are stored completely differently and behave differently. An integer is a plain binary place-value pattern, is exact, and arithmetic on it is fast. A real is stored as a mantissa and an exponent, is only an approximation for most values, and arithmetic on it is slower because the exponents have to be handled as well as the digits. Storing a whole number as a real would waste the range and introduce rounding error for no gain.
- 5
Explain why 0.1 cannot be stored exactly in a real (floating point) variable.
Model answer
Binary fractions can only represent sums of halves, quarters, eighths and so on. Converting 0.1 by repeated multiplication by 2 gives 0.0001100110011... recurring forever, so it never terminates. The computer has a fixed number of mantissa bits, so it stores a truncated version, which is very slightly wrong. The same is true of any denary fraction whose denominator is not a power of two.
- 6
Why is it dangerous to test two real numbers for exact equality?
Model answer
Because rounding errors mean two calculations that should give the same value may differ in the last bits. A total built by adding 0.1 ten times may not equal 1.0 exactly. The safe test is whether the difference between the two values is smaller than a small tolerance, rather than whether they are identical.
Other ways to work this
The same content, four other ways.
What is inside Levo
Six ways to work the same material.
Every question in Data Types and Number Representation, on a schedule.
Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.
Or £46 a year, about £3.83 a month. Cancel anytime.