Data Types Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Data Types.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

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.

Different kinds of data (numbers, text, true/false) work differently.

Read the full concept explanation β†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

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.

Sense of Study hint: When working with data types, always check what type a value is before performing operations on it. If you need to combine different types (e.g., a number and a string), explicitly convert one to match the other. Use your language's type-checking tools to catch mismatches early.

Worked Examples

Example 1

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

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.20.1 + 0.2 equal 0.300000000000000040.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\$3.45. Store it as what integer, and what is the total cents for 44 bags?

Example 7

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

Practice Problems

Try these problems on your own first, 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/27 / 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%310 \% 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-7?

Example 24

easy
What data type is the value 0.00.0?

Example 25

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

Example 26

easy
What type results from the comparison 10>310 > 3?

Example 27

medium
In a language with integer division, what is 20/620 / 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 100100, what conversion is needed, and is the converted value less than 100100?

Example 30

medium
Adding integer 44 and float 1.51.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-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.03.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?

Background Knowledge

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

variable