Practice question · Sort into groups
The symbol + adds numbers but joins text. Sort each expression by what + actually does.
Groups: Adds numbers · Joins text
- 1.5 + 2
- 3 + 4
- the quoted word cat plus the quoted word fish
- the quoted text 3 plus the quoted text 4
- the quoted phrase total: plus the quoted text 7
Hints
- The operands' TYPE decides the operation, not the symbol.
- Quoted digits are still text.
Show the answer
Adds numbers: 3 + 4, 1.5 + 2
Joins text: the quoted text 3 plus the quoted text 4, the quoted word cat plus the quoted word fish, the quoted phrase total: plus the quoted text 7
Why
The numeric cases give 7 and 3.5; the quoted cases give 34, catfish and total: 7. The same symbol means two different operations, which is why knowing a value's type is not optional.
Practise Variables, Types, and Expressions
The app has 4 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.
More questions on Variables, Types, and Expressions
- In most languages 0.1 + 0.2 == 0.3 evaluates to false. Why do experienced programmers compare floats with a…
- Match each data type to its defining property.
- Order the evaluation steps of the expression 2 + 3 * (8 - 6) / 2.
- Select every statement that is TRUE.
- Because a float stores only finitely many digits, two computations that are mathematically equal can produce…
- In mathematics x = x + 1 is a contradiction with no solution. In most programming languages it is a routine…