- Practice
- About 13 min
- 5 practice questions
Variables, data types and operators
Choose data types, evaluate expressions and use integer division and remainder correctly.
If something looks wrong, please tell us.
A variable is a named place in memory that holds a value which may change. Every variable has a data type that tells what kind of value it holds. Expression questions are common, so know your operators.
Common data types
| Type | Holds | Example |
|---|---|---|
| Integer | Whole numbers | 25, -3 |
| Float (real) | Numbers with a fractional part | 3.14 |
| Character | A single letter, digit or symbol | 'A' |
| String | A sequence of characters | "India" |
| Boolean | True or false only | true |
Operators
- Arithmetic: + - * / and % (remainder, called modulus).
- Relational: ==, !=, <, >, <=, >= give true or false.
- Logical: AND, OR, NOT combine true/false values.
- Assignment: = stores a value in a variable, and it is different from == which compares.
Worked examples
Example 1. What are 17 divided by 5 using integer division, and 17 % 5? Integer division gives 3 (since 5 x 3 = 15) and 17 % 5 gives the remainder 2.
Example 2. Let A = 6 and B = 4. What is the value of A * B - A / 2? Multiply and divide first: 6 x 4 = 24 and 6 / 2 = 3. Then 24 - 3 = 21.
= assigns, == compares. % gives the remainder. Multiply and divide before add and subtract.
Key takeaways: match the data type to the value, and do not mix up = and ==.
Check your understanding
-
1. What is 34 % 8?
- 4
- 2
- 4.25
- 6
Show answer
Answer: B. 8 x 4 = 32 and 34 - 32 = 2, so the remainder is 2.
-
2. Let A = 8 and B = 2. What is A * B - A / 2?
- 4
- 12
- 6
- 16
Show answer
Answer: B. Multiply and divide first: 8 x 2 = 16 and 8 / 2 = 4. Then 16 - 4 = 12.
-
3. Which statement correctly describes = and ==?
- = compares two values; == stores a value
- Both store a value
- Both compare two values
- = stores a value in a variable; == compares two values
Show answer
Answer: D. = is assignment and == is comparison. Mixing them up is a common trap.
-
4. X is 9. What does the statement X = X + 1 do?
- It compares X with X + 1 and gives False
- It stores 10 in X
- It stores 1 in X
- It leaves X as 9
Show answer
Answer: B. The right side is worked out first, 9 + 1 = 10, and the result is stored back in X.
-
5. Which data type is best for storing a price of 45.75?
- Integer
- Boolean
- Character
- Float
Show answer
Answer: D. 45.75 has a fractional part, so it needs a float (real) type.
Sign in to keep track of the lessons you finish.