Consider the following code segment. What is printed as a result of executing the code segment?
int level = 6;
level = level + 4;
level = level * 3;
System.out.println(level);
61030186 becomes 10 and then 30 before printing.
Explanation
Java evaluates the right side of each assignment using the value the variable holds at that moment, then stores the result back in that variable. The second statement uses 6 to produce 10, and the third uses 10 to produce 30.
- A is not correct:
6is only the value stored before the second statement runs, and any later assignment replaces the current value. - B is not correct:
10is the value held after the second statement, but the third statement runs and replaces it before anything is printed. - D is not correct: this value would come from multiplying the original
6by3, but the third statement uses the current value10.