Computer Science I / Variables, Expressions, and Types
Practice question · Sort into groups

The operator + adds two numbers but joins two strings. Sort each expression by what it evaluates to.

Groups: Evaluates to a number · Evaluates to a string

Hints
  1. Look at whether the operands are numbers or quoted text before deciding what + does.
  2. "1" + "1" joins two pieces of text; it does not add two numbers.
Show the answer

Evaluates to a number: 3 + 4, 10 - 3, 2 * 5

Evaluates to a string: "3" + "4", "ab" + "cd", "1" + "1"

Why

With numeric operands + adds; with string operands it concatenates. So "3" + "4" is "34" and "1" + "1" is "11", not 7 or 2. The operator is the same symbol; the operand types decide what it means, mixing them carelessly is a frequent source of bugs.

Read the lesson: Variables, Expressions, and Types →

Practise Variables, Expressions, and Types

The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Variables, Expressions, and Types