Data Types Examples: 46 Problems with Answers

Worked examples of Data Types to try before you open each solution, then practice problems with answers.

Worked Examples

Example 1

easy
Classify each value into its data type: (a) 42, (b) 'Hello', (c) 3.14, (d) TRUE.
Show solution

Answer

(a) Integer, (b) String, (c) Float/Real, (d) Boolean.

First step

1.
Step 1: 42 is a whole number with no decimal point — this is an integer.

Full solution

  1. 2.
    Step 2: 'Hello' is text enclosed in quotes — this is a string.
  2. 3.
    Step 3: 3.14 has a decimal part — this is a float (real number). TRUE is a logical value — this is a boolean.
Data types define what kind of value a variable holds and what operations can be performed on it. The four fundamental types are integer, float, string, and boolean.

Example 2

medium
A program reads user input as a string '25'. What happens if you try to calculate '25' + 10 without type conversion? What should you do instead?

Example 3

easy
Classify each: (a) 100, (b) 'OK', (c) 2.5, (d) true, (e) [1,2].

Example 4

medium
For each scenario, choose the most appropriate data type: (a) phone number, (b) age in years, (c) account balance in dollars, (d) is_member?

Example 5

hard
Why might 0.1+0.2 equal 0.30000000000000004 in floating point, and what is one common workaround?

Example 6

hard
A shop tracks prices as integer cents to avoid floating-point error. A bag of apples costs $3.45. Store it as what integer, and what is the total cents for 4 bags?

Example 7

challenge
Sorting strings ['9','10','100','2'] lexicographically vs numerically gives different orders. Show both sorted lists.

Practice Problems

Try each one, then open the solution to compare your method.

Example 1

medium
For each scenario, choose the most appropriate data type: (a) a student's name, (b) number of students in a class, (c) whether a student passed, (d) a student's average grade (e.g., 87.5).

Example 2

hard
A program stores prices as integers in pence (e.g., 499 for £4.99) rather than as floats. Explain two advantages of this approach.

Example 3

easy
What data type is the value true?

Example 4

easy
What data type is the value 'hello'?

Example 5

easy
What data type is the value 42?

Example 6

easy
What data type is the value 3.14?

Example 7

easy
Which operation joins two strings: addition (concatenation) or subtraction?

Example 8

easy
Are '5' (a string) and 5 (an integer) the same data type? Yes or no.

Example 9

easy
What type results from comparing 3 < 5?

Example 10

easy
What data type is a collection like [1, 2, 3]?

Example 11

medium
In a language with integer division, what is 7/2 as integer division?

Example 12

medium
What is the result of '3' + '4' when both are strings?

Example 13

medium
User input arrives as the string '10'. To add 5 numerically, what must you do first, and what is the result?

Example 14

medium
What is 10%3 (the modulo/remainder operation)?

Example 15

medium
Adding an integer 5 and a float 2.5 gives what type and value?

Example 16

medium
A list stores [1, 'two', 3.0]. What are the types of its three elements in order?

Example 17

medium
Why does comparing 0.1 + 0.2 == 0.3 sometimes return false in floating-point?

Example 18

medium
What is the length (number of characters) of the string 'hello'?

Example 19

medium
Index into the string 'CODE' at position 0 (zero-based). What character is returned?

Example 20

challenge
A program reads ['3','1','2'] (strings) and sorts them; then reads [3,1,2] (ints) and sorts. Both sort fine here, but when would string vs int sorting differ? Give the order for ['10','9','2'].

Example 21

challenge
A function expects an integer count but receives the float 4.0. It uses count as a list size. Why might this error, and what is the safe fix?

Example 22

challenge
Booleans are often treated as numbers: true=1, false=0. What does true + true + false evaluate to under this rule?

Example 23

easy
What data type is the value −7?

Example 24

easy
What data type is the value 0.0?

Example 25

easy
Which operation joins 'cat' + 'fish' into 'catfish'?

Example 26

easy
What type results from the comparison 10>3?

Example 27

medium
In a language with integer division, what is 20/6?

Example 28

medium
What is the result of '7' + '8' when both are strings?

Example 29

medium
User input '99' arrives as a string. To compare it numerically against 100, what conversion is needed, and is the converted value less than 100?

Example 30

medium
Adding integer 4 and float 1.5 gives what type and value?

Example 31

medium
What is the length of the string 'computer'?

Example 32

medium
Zero-based index 2 of 'PYTHON' returns which character?

Example 33

medium
What is the result of '5' * 3 in a language where string * int repeats the string?

Example 34

medium
List the types of the elements of [42, 'cat', 3.0, false] in order.

Example 35

hard
In integer division, what is −7//2 if the language rounds toward negative infinity?

Example 36

hard
A program reads ['100','20','3'] as strings and sorts them lexicographically. Give the sorted order.

Example 37

hard
A function expects an integer index but receives the float 3.0. It uses the index for list[i]. Why is this often an error, and what is the safe fix?

Example 38

hard
Booleans true=1, false=0. Evaluate true + false + true + true.

Example 39

challenge
A function signature is f(name: string, age: int) -> bool. It is called as f('Sam', '14'). What is the type-error and what conversion would fix it?

What to focus on

Core idea: Operations depend on type: you can add numbers, concatenate strings.

Common stuck point: '5' (text) and 5 (number) are different—some languages convert automatically, some don't.

Ask yourself: Does the result depend on what kind of value it is, such as a number, text, or true/false?

Concept recap

Categories that classify data values and determine which operations can validly be performed on them. Common data types include integers, floating-point numbers, strings, booleans, and arrays, each with its own set of permitted operations.

Data types are like the units on a measuring jug and a ruler. Both show numbers, but adding 3 cups to 3 centimetres makes no sense, so you check what each number measures before combining them. A computer checks types the same way. The comparison stops at strings: text measures nothing, and joining two pieces of text just sets one after the other.

Read the full lesson on Data Types

Background knowledge

These ideas may be useful before you work through the harder examples.

variable