Expressions Practice#
These questions are to assess your understanding of Java expressions.
For each question, answer with the value that would result from fully evaluating that expression. Remember some of the tricky aspects of expressions in Java:
[!] [P] [MMD] [AS] [<><=>=] [== !=] [&&] [||] precedence!
When there are multiple operations at the same level of precedence, they are executed left to right in order.
Keep track of the type of each subexpression as you work through each problem!
Watch out for that pesky
int
division!
Note
Make sure your answer conveys the appropriate type of the result in each answer. For double results, include the decimal point (even if the answer is a whole number, such as 7.0 instead of 7). For String results, surround the value with double-quotes (such as “hi 23”).
Questions 1
3 * 10
See Answer
30
Question 2
"cat" + "dog"
See Answer
“catdog”
Remember that, when dealing with String
s, the +
operator performs concatenation! Also keep in mind that concatenation won’t add any spacing for you— since neither String contains any space characters, the ending result doesn’t have any space characters either, which is why we get "catdog"
rather than "cat dog"
!
Questions 3
11 % 3
See Answer
2
The mod operator (%
) gives us the remainder after dividing 11
by 3
. 3
fits into 11
three times evenly, which leaves a remainder of 2
!
Question 4
(2 + 3 * 6) % 3
See Answer
2
Let’s break this down! The underlined subexpression indicates the next step of evaluating the entire expression. Each step is performed on its own line.
(2 + 3 * 6) % 3
(2 + 18) % 3
20 % 3
2
Questions 5
12 / 5 + 8 / 4
See Answer
4
Remember that pesky int
division!
The underlined subexpression indicates the next step of evaluating the entire expression. Each step is performed on its own line.
12 / 5 + 8 / 4
2 + 8 / 4
2 + 2
4
Question 6
(14 / 5 + 7 * 3) % 8 > 4
See Answer
true
Now we’re also dealing with a relational operator! Remember your order of operations and take things one step at a time.
The underlined subexpression indicates the next step of evaluating the entire expression. Each step is performed on its own line.
(14 / 5 + 7 * 3) % 8 > 4
(2 + 7 * 3) % 8 > 4
(2 + 21) % 8 > 4
23 % 8 > 4
7 > 4
true
Questions 7
12.0 / 5
See Answer
2.4
When mixing types int
and double
in an expression, remember that Java will convert the int
to a double
, then perform the operation as if it were working with two double
!
12.0 / 5 --> 12.0 / 5.0 --> 2.4
Question 8
"hello" + 2 + 3
See Answer
“hello23”
When mixing types int
and String
in an expression, Java will convert the int
s to String
s, then perform the operation as if it were working with two String
s!
The underlined subexpression indicates the next step of evaluating the entire expression. Each step is performed on its own line.
”hello” + 2 + 3
”hello” + “2” + 3
”hello2” + 3
”hello2” + “3”
“hello23”
Question 9
2 + 3 + "hello"
See Answer
“5hello”
Since we have two +
operators that share the same level of precedence, Java will perform each operation left to right in order! And when mixing types int
and String
in an expression, Java will convert the int
s to String
s, then perform the operation as if it were working with two String
s!
The underlined subexpression indicates the next step of evaluating the entire expression. Each step is performed on its own line.
2 + 3 + “hello”
5 + “hello”
”5” + “hello”
“5hello”