• 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

Data types
TypeHoldsExample
IntegerWhole numbers25, -3
Float (real)Numbers with a fractional part3.14
CharacterA single letter, digit or symbol'A'
StringA sequence of characters"India"
BooleanTrue or false onlytrue

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.

— Rule to remember

Key takeaways: match the data type to the value, and do not mix up = and ==.

Check your understanding

  1. 1. What is 34 % 8?

    1. 4
    2. 2
    3. 4.25
    4. 6
    Show answer

    Answer: B. 8 x 4 = 32 and 34 - 32 = 2, so the remainder is 2.

  2. 2. Let A = 8 and B = 2. What is A * B - A / 2?

    1. 4
    2. 12
    3. 6
    4. 16
    Show answer

    Answer: B. Multiply and divide first: 8 x 2 = 16 and 8 / 2 = 4. Then 16 - 4 = 12.

  3. 3. Which statement correctly describes = and ==?

    1. = compares two values; == stores a value
    2. Both store a value
    3. Both compare two values
    4. = 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. 4. X is 9. What does the statement X = X + 1 do?

    1. It compares X with X + 1 and gives False
    2. It stores 10 in X
    3. It stores 1 in X
    4. 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. 5. Which data type is best for storing a price of 45.75?

    1. Integer
    2. Boolean
    3. Character
    4. Float
    Show answer

    Answer: D. 45.75 has a fractional part, so it needs a float (real) type.

Privacy · Terms · Refunds · Delivery