DifferenceEasy2 marks
Write one difference between / and % operator.
Topic: Division and modulus operators
Previous-year question practice database
Answer
Exam answer
The difference is shown in the table.
Explanation
State quotient versus remainder and give the same pair of numbers through both operators — the parallel example makes the answer unmistakable. One further point worth a line: with two int operands, / performs INTEGER division and discards the fraction, so 17 / 5 is 3 and not 3.4. To obtain 3.4 one operand must be a double.
Comparison
| / (division) | % (modulus) |
|---|---|
| Gives the quotient when one number is divided by another. | Gives the remainder when one number is divided by another. |
| Example: 17 / 5 gives 3 | Example: 17 % 5 gives 2 |
More from Revision Tour I
The full form of JVM is:2026Which of the following occupies 2 bytes of storage?2026In a statement c = c + (x * d + e); which variable is an accumulator?2026Assertion (A): The result of the Java expression 3 + 7/2 is 6.
Reason (R): According to the hierarchy of operators in Java, addition is done first followed by division.2026Evaluate the Java expression:
x = a * b % (++c) + (++a) + (--b);
if a = 7, b = 8, c = 22026System.out.println('Z' + 32); will display:2025