Operator, Division and Overflow Puzzles
40 min · Intermediate
Most "what is the output?" questions on operators test a small set of rules: when ++ takes effect, how compound assignment casts, how integer division rounds, which way + groups with strings, and what happens when an int overflows or a double cannot store 0.1 exactly. Learn the rules, then trace every puzzle on paper instead of guessing.
Topics in this lesson
- How to trace: write variable values line by line
- i++ + ++i and compound assignment casts
- Integer division, % with negatives, char and String order
- Overflow: Integer.MAX_VALUE + 1 and multiplying before widening
- Floating point: 0.1 + 0.2 and == on doubles
Interview questions this lesson answers
- What is the difference between i++ and ++i?
- What does i = i++ do?
- What is the value of i++ + ++i when i is 5?
- Is the order of evaluation defined in Java, unlike in C?
- Why does byte b = 10; b = b + 5; not compile, but b += 5 does?
- Can compound assignment hide a bug?
- What is numeric promotion?
- How does integer division round?
- What sign does % give with negative numbers?
- What happens when you divide by zero?
Sign in free to continue this lesson
Create a free account to read every lesson, practise with the exercises and keep track of your progress.