Guys! Due to some maintenance issue, you may find issues while submitting code. Inconvenience is deeply regretted. we will fix this issue asap.

Variables In Java

What is the output of following code?

int a = 10;
int b = 20;
a = a + b;
b = a - b;
a = a - b;

System.out.println(a + " " + b);
  • View Correct Answer
    Answer: 20 10
     int a = 10; // assigns 10 to a
    int b = 20; // assigns 20 to b

    a = a + b; // a is now 30;
    b = a - b; // b = 30 - 20, b is now 10 and a stays 30
    a = a - b; // a = 30 - 10, a is now 20 and b stays 10
 
Error:
Output
RUN
Id Name