With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

Chapter 4: Operators

Quiz Solutions

Solution to Question 4-1. The `=` operator is the assignment operator, used to assign a value to a variable. The `==` operator is the equality operator, which tests the equality of two values and returns a Boolean. Confusing the two is a very common mistake, and a common source of errors.

Solution to Question 4-2. To assign the same value to multiple variables, simply chain the assignments, like this:

`int a = b = c = d = 36;`

Solution to Question 4-3. When you divide two `double`s, the solution has a fractional portion, expressed as a decimal, as you would expect. When you divide two `int`s, the compiler discards any fractional remainder.

Solution to Question 4-4. The purpose of the `%` operator is to return the remainder from an integer division. It’s very useful in controlling loops, as you’ll see later.

Solution to Question 4-5. The output of the operations is:

• 32

• 6

• 4 (Be careful of the order of operations here; the division (`8 / 4`) takes place before the addition and the subtraction)

Be sure to take note of the parentheses and the order of operator precedence, as discussed in Operator Precedence.

Solution to Question 4-6. Because the self-assignment operators are used here, the value of `myInt` changes with each step, forming a new input for the next step.

```myInt += 5;
myInt = 30
myInt -= 15;
myInt = 15
myInt *= 4;
myInt = 60
myInt /= 3;
myInt = 20```

Solution to Question 4-7. The prefix operator increments (or decrements) the original value, and then assigns the new value ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required