please find out the output.
Answers
Required Answer:-
Question:
- Find the output of the following códe snippet.
Output:
8, 7
Explanation:
Given códe snippet,
class Q2
{
public static void main()
{
int a=7;
int b=8;
a=a+b;
b=a-b;
a=a-b;
System.out.print(a+", "+b);
}
}
Here, we can see that,
a = 7 and b = 8 (initially)
a = a + b
Here, a is now equal to sum of a and b,
➡ a = 7 + 8
➡a = 15
a becomes 15
b = a - b
Here, b is now equal to difference of a and b,
➡ b = 15 - 8 (a = 15 and b = 8 before)
➡ b = 7
b becomes 7
a = a - b
Here, a is now equal to difference of a and b,
➡ a = 15 - 7 (a = 15 and b = 7 before)
➡ a = 8
a becomes 8
Hence,
a = 8
b = 7
System.out.print(a+", "+b);
Now, values of a and b are displayed in same line separated by comma.
Output: 8, 7
Output:
8, 7
Given Snippet
int a = 7, b = 8;
a = a + b;
b = a - b;
a = a - b;
System.out.print(a + ", " + b);
So, as a = 7 and b = 8,
First,
a = a + b or,
a = 7 + 8
a = 15
Secondly,
b = a - b, now as a is now 15, so,
b = 15 - 8
b = 7
Lastly,
a = a - b, as b is now 7 and a is 15, so,
a = 15 - 7
a = 8
Finally, a = 8 and b = 7.