Question:

What will be the result of arithmetic expression –7%2 in Java?

Show Hint

In Java, the modulus operator {%} returns the remainder and keeps the {sign of the dividend}.
  • -3
  • -1
  • 1
  • -3.5
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Understanding the modulus operator.
In Java, the symbol % represents the modulus operator. The modulus operator is used to find the remainder after division of one number by another.
For example:
10 % 3 = 1 because when 10 is divided by 3, the remainder is 1.
Step 2: Evaluating the given expression.
The expression given is:
-7 % 2
First divide 7 by 2.
\[ 7 \div 2 = 3 \text{ remainder } 1 \] Now apply the sign rule in Java. The result of the modulus operation takes the sign of the dividend. In this case, the dividend is -7.
Therefore:
\[ -7 % 2 = -1 \] Step 3: Checking the options.
(A) -3: Incorrect because modulus gives remainder, not quotient.
(B) -1: Correct remainder when -7 is divided by 2.
(C) 1: Incorrect because Java keeps the sign of the dividend.
(D) -3.5: Incorrect because modulus operation does not produce decimal values.
Step 4: Conclusion.
Thus, the result of the arithmetic expression -7%2 in Java is -1.
Final Answer: -1
Was this answer helpful?
0
0