Computer Applications

Write the value of n and m after execution of the following code.

int m;
int n;
m = 5;
n = (5 * ++m) % 3;
System.out.println("n = " + n + "m =" + m);

Java Operators

8 Likes

Answer

Output
n = 0m = 6
Explanation

The statement n = (5 * ++m) % 3; is evaluated as follows:

n = (5 * ++m) % 3; [m = 5]

⇒ n = (5 * 6) % 3; [Prefix operator first increments the value and then uses it. So, m = 5 + 1 = 6 and then it is used in the expression]

⇒ n = 30 % 3;

⇒ n = 0;

So, final value of m is 6 and n is 0.

Answered By

5 Likes


Related Questions