Assignment operators based Output Writing – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Solved Problem #JAVA#3672 siteicon   siteicon   siteicon  

Problem Statement - Assignment operators based Output Writing

Write output of the following program instructions.

int a=10;
int b=a=12;
System.out.println(a+b);
b%=5;
a/=b;
System.out.println(a+b);

Solution

TC++ #3672

Run Output

24
8

int a=10; //integer variable a initialised with 10
int b=a=12;
Integer variable b declared and subsequently initialised with value of a which is also given another value 12. So both a and b will become 12.

System.out.println(a+b);
This will print 24 as both a and b are now 12.

b%=5;
This is equivalent to b=b%5 which means remainder of 12/5 will be filled in b. which means b would become 2.

a/=b;
This will be equivalent to a=a/b. Since a is 12 and b is now 2 so after this computation a would become 6

System.out.println(a+b);
As a is now 6 and b is 2 so this would print 8

Notes

  • writing like int b=a=12; is feasible only when a has been declared before.


Share

sunmitra| Created: 2-Mar-2018 | Updated: 27-Nov-2019|






Back