1Z0-809 · Question #135
Given: ``java public class App { public static void main(String[] args) { int i = 10; int j = 20; int k = i++ + j / 5 + i; System.out.print(i + ":" + j + ":" + k); } } `` What is the result?
The correct answer is B. 11: 20: 25. Option B is correct because Java's post-increment operator (i++) uses the current value of i (10) in the expression, then increments i to 11 afterward - so the expression evaluates left-to-right as 10 + (20/5) + 11 = 10 + 4 + 11 = 25, with the final print producing 11:20:25…
Question
public class App {
public static void main(String[] args) {
int i = 10;
int j = 20;
int k = i++ + j / 5 + i;
System.out.print(i + ":" + j + ":" + k);
}
}
What is the result?Options
- A10: 22: 20
- B11: 20: 25
- C10: 30: 24
- D10: 22: 6
How the community answered
(47 responses)- A4% (2)
- B81% (38)
- C4% (2)
- D11% (5)
Explanation
Option B is correct because Java's post-increment operator (i++) uses the current value of i (10) in the expression, then increments i to 11 afterward - so the expression evaluates left-to-right as 10 + (20/5) + 11 = 10 + 4 + 11 = 25, with the final print producing 11:20:25. Options A and D are wrong because they incorrectly show i as 10 in the output, ignoring that post-increment has already fired by the time print runs - and their values of 22 for j are fabricated, since j is never modified. Option C is wrong because it also keeps i at 10 in the output and inflates j to 30, confusing j / 5 (integer division = 4) with some imagined assignment. Memory tip: think of i++ as "pay now, charge later" - the expression gets the old value (10), but i is already 11 for any code that follows, including within the same expression to its right.
Community Discussion
No community discussion yet for this question.