levo

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.

Your recallNot started
Key term

What does a variable's data type actually tell the computer?

Numbers

A shop program stores prices. Would you use a real to hold 19.99, and why?

Key term

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. 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. 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. 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. 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. 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. 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.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

A0 + 0 gives 0.
B0 + 1 gives 1.
C1 + 1 gives 0 carry 1.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

A global variable means a variable reachable from anywhere in the program.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

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.