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
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
sunmitra| Created: 2-Mar-2018 | Updated: 27-Nov-2019|
Post Views:
572