Sure, here's an AP Computer Science Exam question for the given topic:
Question: Consider the following code snippet:
int x = 5;
int y = 3;
int z = x * y + 4 / 2;
System.out.println("z = " + z);
What is the value of z
after the code snippet is executed? Show your work.
A) 16
B) 17
C) 20
D) 21
E) 22
Answer: The correct answer is C) 20.
Explanation:
To determine the value of z
, we need to follow the order of operations. First, we compute x * y
, which is 5 * 3 = 15
. Then, we move on to the addition and division operations. Since addition and division have the same precedence, we evaluate from left to right.
Next, we calculate 4 / 2
, which is 2
.
Finally, we add the result of x * y
to the result of 4 / 2
, giving us 15 + 2 = 17
.
Therefore, the value of z
is 17
.
Therefore, the correct answer is C) 20.