Detailed explanation
The program prints three lines, in order: 3, 3.3333333333333335, and 1. Each expression is evaluated before its result is passed to System.out.println, which prints the value followed by a line break. The important distinction is the type of each operand: Java uses these types to decide whether division should use integer arithmetic or floating-point arithmetic. The % operator calculates a remainder rather than a quotient.
In 10 / 3, both literals have type int, so Java performs integer division and produces an int result. The mathematical quotient is approximately 3.333..., but integer division discards the fractional part, leaving 3. More precisely, Java truncates an integer quotient toward zero; it does not round to the nearest integer or always round downward. For example, -10 / 3 produces -3, not -4. This distinction matters when working with negative values or implementing calculations such as averages, where an unexpected integer division can silently lose information.
In 10.0 / 3, the literal 10.0 has type double. Java's binary numeric promotion therefore converts the int operand 3 to double before division, making the operation equivalent to 10.0 / 3.0. The result has type double and is printed as 3.3333333333333335. A double uses IEEE 754 binary floating-point representation with finite precision, so it cannot represent the exact fraction 10/3. The printed value is the decimal representation Java uses for the resulting double. Importantly, assigning 10 / 3 to a double variable would produce 3.0: the integer division happens first, and the conversion cannot restore the discarded fraction.
Finally, 10 % 3 produces 1 because dividing 10 by 3 gives a quotient of 3 with 1 left over: 10 = 3 * 3 + 1. Interviewers use this question to check operand types, numeric promotion, truncation, and the distinction between division and remainder. A strong answer states the output first, then explains why changing just one operand to double changes the division result.
Key points to remember
- Output on separate lines: 3, 3.3333333333333335, and 1.
- Two int operands make / perform integer division.
- Integer division truncates toward zero, including for negative quotients.
- A double operand promotes the other int operand to double before division.
- The % operator returns the remainder; 10 % 3 is 1.
Common mistakes candidates make
- Expecting 10 / 3 to produce a fractional result because the mathematical quotient is fractional.
- Assuming double result = 10 / 3 preserves the fraction; it actually stores 3.0.
- Describing integer division as rounding down, which gives the wrong explanation for negative quotients.
Likely follow-up questions
How can you get floating-point division when both variables are int?
Convert at least one operand before division, for example, (double) a / b. Casting the result with (double) (a / b) is too late because integer division has already discarded the fractional part.
What happens when the divisor is zero?
Integer division or remainder with a zero divisor throws ArithmeticException when evaluated. Floating-point division follows IEEE 754 rules: 10.0 / 0.0 produces positive infinity, while 0.0 / 0.0 produces NaN.
What do -10 / 3 and -10 % 3 produce?
-10 / 3 produces -3 because integer division truncates toward zero. -10 % 3 produces -1; a nonzero integer remainder has the same sign as the dividend.
Interview tip: State all three output lines first, then identify the operand types and explain each operation in one clear sentence.
