Chapter:Expressions and operators - Exercise

From Juneday education
Jump to: navigation, search

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.

Answer:
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.

Answer:
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.

Answer:
-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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

Answer:
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.

  1. 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.

Answer:
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.

Answer:
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

« PreviousBook TOCNext »