# Chapter:Expressions and operators - Exercise

## Contents

- 1 Exercises on operators and expressions
- 1.1 Operators
- 1.1.1 Q-Op-1
- 1.1.2 Q-Op-2
- 1.1.3 Q-Op-3
- 1.1.4 Q-Op-4
- 1.1.5 Q-Op-5
- 1.1.6 Q-Op-6
- 1.1.7 Q-Op-7
- 1.1.8 Q-Op-8
- 1.1.9 Q-Op-9
- 1.1.10 Q-Op-10
- 1.1.11 Q-Op-11
- 1.1.12 Q-Op-12
- 1.1.13 Q-Op-13
- 1.1.14 Q-Op-14
- 1.1.15 Q-Op-15
- 1.1.16 Q-Op-16
- 1.1.17 Q-Op-17
- 1.1.18 Q-Op-18
- 1.1.19 Q-Op-19
- 1.1.20 Q-Op-20
- 1.1.21 Q-Op-21
- 1.1.22 Q-Op-22

- 1.2 Expressions

- 1.1 Operators
- 2 Links

# Exercises on operators and expressions

## Operators

### Q-Op-1

What is the value after the following operation has been evaluated/calculated?

```
7 + 9
```

Expand using link to the right to see a hint.

```
16
```

### Q-Op-2

What is the value after the following operation has been evaluated/calculated?

```
7 * 9
```

Expand using link to the right to see a hint.

```
63
```

### Q-Op-3

What is the value after the following operation has been evaluated/calculated?

```
7 - 9
```

Expand using link to the right to see a hint.

```
-2
```

### Q-Op-4

What is the value after the following operation has been evaluated/calculated?

```
7 / 3
```

Expand using link to the right to see a hint.

```
2
```

Think of this as splitting 7 oranges between 3 persons and you're only allowed to hand out complete or whole oranges. Each person would get two oranges. There will however be one orange left over. In Java, when using division with integers (whole numbers), the result is a whole number. Any decimals are "truncated away".

### Q-Op-5

What is the value after the following operation has been evaluated/calculated?

```
8 / 2
```

Expand using link to the right to see a hint.

```
4
```

### Q-Op-6

What is the value after the following operation has been evaluated/calculated?

```
9 / 2
```

Expand using link to the right to see a hint.

```
4
```

### Q-Op-7

What is the value after the following operation has been evaluated/calculated?

```
7 % 3
```

Expand using link to the right to see a hint.

```
1
```

This is the remaining orange from exercise (Q-Op-4 above).

### Q-Op-8

What is the value after the following operation has been evaluated/calculated?

```
8 % 3
```

Expand using link to the right to see a hint.

```
2
```

If each of the three persons get 2 oranges, it means six oranges are given away. Following that there are 2 left over.

### Q-Op-9

What is the value after the following operation has been evaluated/calculated?

```
8 * 3 + 2
```

Expand using link to the right to see a hint.

```
26
```

Multiplications are calculated before additions, so first 8*3, which is 24, and than add 2, which gives us 26

### Q-Op-10

What is the value after the following operation has been evaluated/calculated?

```
8 + 3 * 2
```

Expand using link to the right to see a hint.

```
14
```

Multiplications are calculated before additions, so first 3*2, which is 6, and than add 8, which gives us 14.

### Q-Op-11

What is the value after the following operation has been evaluated/calculated?

```
(8 * 3) + 2
```

Expand using link to the right to see a hint.

```
26
```

The operations inside parentheses are calculated before other operations, so first 8*3, whch is 24, and than add 2, which gives us 26. Note that the parentheses are not necessary here, since multiplication would be done first even without them.

### Q-Op-12

What is the value after the following operation has been evaluated/calculated?

```
8 * (3 + 2)
```

Expand using link to the right to see a hint.

```
40
```

The operations inside parentheses are calculated before other operations, so first 3+2, which is 5, and than multiply with 8, which gives us 40. Without the parentheses, the result would be different. Can you figure out what it would be?

### Q-Op-13

What is the resulting value of the following comparison expression?

```
12 > 12
```

Expand using link to the right to see a hint.

```
false
```

12 is not bigger than 12, so false. Think of the expression as a claim that 12 is greater than 12. This claim is not true, so it is false.

### Q-Op-14

What is the resulting value of the following expression?

```
12 > 90
```

Expand using link to the right to see a hint.

```
false
```

12 is not bigger than 90, so false.

### Q-Op-15

What is the resulting value of the following expression?

```
12 >= 12
```

Expand using link to the right to see a hint.

```
true
```

12 is not bigger than 12 but it is equal to 12, so true. In other words: It is true that 12 is greater than OR equal to 12.

### Q-Op-16

What is the resulting value of the following expression?

```
12 >= 90
```

Expand using link to the right to see a hint.

```
false
```

12 is not greater than 90 or equal to 90, so false.

### Q-Op-17

What is the resulting value of the following expression?

```
12 < 12
```

Expand using link to the right to see a hint.

```
false
```

12 is not less than 12, so false.

### Q-Op-18

What is the resulting value of the following expression?

```
12 <= 90
```

Expand using link to the right to see a hint.

```
true
```

12 is less than 90 (although not equal to 90), so true. In other words it is true that 12 is less than or equal to 90.

### Q-Op-19

What is the resulting value of the following expression?

```
12 > 12
```

Expand using link to the right to see a hint.

```
false
```

12 is not greater than 12, so false.

### Q-Op-20

What is the resulting value of the following expression?

```
12 > 90
```

Expand using link to the right to see a hint.

- Answer:
`false`

12 is not greater than 90, so false.

### Q-Op-21

What is the resulting value of the following expression?

```
12 >= 12
```

Expand using link to the right to see a hint.

```
true
```

12 is not greater than 12 but it is equal to 12, so true. In other words: It is true that 12 is greater than OR equal to 12.

### Q-Op-22

What is the resulting value of the following expression?

```
12 >= 90
```

Expand using link to the right to see a hint.

```
false
```

12 is not greater than or equal to 90 so false.

## Expressions

### Q-Expr-1

What is the resulting value of the following expression?

```
1
```

Expand using link to the right to see a hint.

```
1
```

Yeah, it is that simple!

### Q-Expr-2

What is the resulting value of the following expression?

```
1 + 2
```

Expand using link to the right to see a hint.

```
3
```

Still easy!

### Q-Expr-3

What is the resulting value of the following expression?

```
1 + 2 + 234
```

Expand using link to the right to see a hint.

```
237
```

Harder, but still easy!

### Q-Expr-4

What is the resulting value of the following expression?

```
1 + 2 * 3
```

Expand using link to the right to see a hint.

```
7
```

Harder, but still easy! 2 * 3 is evaluated before the addition.

### Q-Expr-5

What is the resulting value of the following expression?

```
(1 + 2) * 3
```

Expand using link to the right to see a hint.

```
9
```

Harder, but still easy! Parentheses are always evaluated before anything else.

# Links

## Further reading

## Where to go next

Next chapter is: Control flow

« Previous • Book TOC • Next »