Increment, Decrement operator Output Writing – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Solved Problem #JAVA#3666 siteicon   siteicon   siteicon  

Problem Statement - Increment, Decrement operator Output Writing

Write output of the following program instructions.

int a=10;
System.out.println(a++);
int b = –a;
System.out.println(b);
b= b++ – -a;
System.out.println(b++);
a= –b + ++a;
System.out.println(a);

Solution

TC++ #3666

Run Output

10
10
20
31

int a=10; //a initialised with 10

System.out.println(a++);
This will print 10 only as a++ will have its effect in next statement

int b = –a;
Here b will become 10 again as previous a++ will make a as 11 but here prefix decrement will make it 10 again.

System.out.println(b);
Here 10 will be printed.

b= b++ – -a;
This will become 10 – (-10) so it will become 10+10 so b will contain 20 now.

System.out.println(b++);
This will print 20 as effect of postfix will not come here. Here b will become 21 after printing.

a= –b + ++a;
Here b will become 20 due to prefix decrement of 21 after the previous statement. so this would be 20+11 (++a will make a to be 11)

System.out.println(a);
So this should print 31


Share

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






Back